Sfoglia il codice sorgente

Moved form elements from views module to Tripal core and updated seq extract form to use the file upload combo element. Also renamed include files to have a .inc extension rather than a .php extension in the core moduel

spficklin 12 anni fa
parent
commit
079f9f0375

+ 9 - 2
tripal_core/api/tripal_core_ahah.api.inc

@@ -44,7 +44,7 @@ function tripal_core_ahah_init_form() {
  * See http://tripal.info/documentation/ahah_api for example usage
  *
  * @returns
- *   A Drupal form array 
+ *   A Drupal form array.  If no form is available then FALSE is returned
  *
  * @ingroup tripal_ahah_api
  */
@@ -53,10 +53,17 @@ function tripal_core_ahah_prepare_form() {
   // Retrieve the form from the cache
   $form_state = array('storage' => NULL);
   $form_build_id = filter_xss($_POST['form_build_id']);
+  if (!$form_build_id) {
+    return FALSE;
+  }
   $form = form_get_cache($form_build_id, $form_state);
 
   // Preparing to process the form
-  $args = $form['#parameters'];
+  $args = $form['#parameters']; 
+  if (!is_array($args)) {
+    // if there is no form #parameters as an array then the form was not built property
+    return FALSE;
+  }  
   $form_id = array_shift($args);
   $form_state['post'] = $form['#post'] = $_POST;
   $form['#programmed'] = $form['#redirect'] = FALSE;

+ 0 - 0
tripal_core/includes/chado_install.php → tripal_core/includes/chado_install.inc


+ 0 - 0
tripal_core/includes/custom_tables.php → tripal_core/includes/custom_tables.inc


+ 65 - 31
tripal_views/includes/tripal_views_form_elements.inc → tripal_core/includes/form_elements.inc

@@ -8,11 +8,11 @@
 /**
  * Register form elements
  */
-function tripal_views_elements() {
+function tripal_core_elements() {
 
   $type['file_upload_combo'] = array(
-    '#input' => TRUE,
-    '#process' => array('expand_file_upload_combo'),
+    '#input'            => TRUE,
+    '#process'          => array('expand_file_upload_combo'),
     '#element_validate' => array('file_upload_combo_validate'),
   );
   
@@ -69,6 +69,61 @@ function expand_file_upload_combo($element, $edit, $form_state, $complete_form)
 
   return $element;
 }
+
+
+/**
+ * Theme the file upload combo form element
+ */
+function theme_file_upload_combo($element) {
+  return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
+}
+
+
+/**
+ * Validate all content passed into the file upload combo form element
+ */
+function file_upload_combo_validate($element, &$form) {
+  
+  $values = array();
+  
+  // get the items in the textbox
+  $items =  $form['values'][$element['#name']]['items'];
+  if ($items) {
+    // split on new line or comma
+    $vals  = preg_split("/[\n,]+/", $items);
+    // iterate through the values and trim surrounding space
+    foreach ($vals as $i => $value) {
+      $values[] = trim($value);
+    }
+  }
+
+  // merge any items from the file upload
+  $file = file_save_upload($element['#name'], array());
+  if ($file) {
+    $file_path = $file->filepath;
+    
+    $form['values'][$element['#name']]['file_path'] = $file_path;
+    // we need to add our file path to the $_GET element as if it were
+    // submitted along with the rest of the form
+    $_GET[$element['#name']]['file_path'] = $file_path;
+
+    $fh = fopen($file_path, 'r');
+    while ($line = fgets($fh)) {
+      $items = trim($line);
+
+      // split on new line or comma
+      $vals  = preg_split("/[\n,]+/", $items);
+      // iterate through the values and trim surrounding space
+      foreach ($vals as $i => $value) {
+        $values[] = trim($value);
+      }
+    }       
+  }
+  
+  // add a new 'items_array' element that contains the array of 
+  // submitted items from both the textbox and the input file
+  $form['values'][$element['#name']]['items_array'] = $values;
+}
 /**
  * Form element description
  */
@@ -105,34 +160,6 @@ function expand_sequence_combo($element, $edit, $form_state, $complete_form) {
   );
   return $element;
 }
-
-/**
- * Theme the file upload combo form element
- */
-function theme_file_upload_combo($element) {
-  return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
-}
-
-/**
- * Theme the file sequence form element
- */
-function theme_sequence_combo($element) {
-  return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
-}
-
-/**
- * Validate all content passed into the file upload combo form element
- */
-function file_upload_combo_validate($element, &$form) {
-  $file = file_save_upload($element['#name'], array());
-  if ($file) {
-    $form['values'][$element['#name']]['file_path'] = $file->filepath;
-    // we need to add our file path to the $_GET element as if it were
-    // submitted along with the rest of the form
-    $_GET[$element['#name']]['file_path'] = $file->filepath;
-  }
-}
-
 /**
  * Validate all content passed into the sequence combo form element
  */
@@ -155,3 +182,10 @@ function sequence_combo_validate($element, &$form) {
     form_set_error($element['#name'], 'Please provide a decimal number for downstream bases');
   }
 }
+
+/**
+ * Theme the file sequence form element
+ */
+function theme_sequence_combo($element) {
+  return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
+}

+ 0 - 0
tripal_core/includes/jobs.php → tripal_core/includes/jobs.inc


+ 0 - 0
tripal_core/includes/mviews.php → tripal_core/includes/mviews.inc


+ 5 - 4
tripal_core/tripal_core.module

@@ -45,10 +45,11 @@ require_once "api/tripal_core_custom_tables.api.inc";
 require_once "api/tripal_core_jobs.api.inc";
 require_once "api/tripal_core_mviews.api.inc";
 require_once "api/tripal_core_misc.api.inc";
-require_once "includes/jobs.php";
-require_once "includes/mviews.php";
-require_once "includes/custom_tables.php";
-require_once "includes/chado_install.php";
+require_once "includes/jobs.inc";
+require_once "includes/mviews.inc";
+require_once "includes/custom_tables.inc";
+require_once "includes/chado_install.inc";
+require_once "includes/form_elements.inc";
 
 /**
  * Implements hook_init().

+ 84 - 92
tripal_feature/includes/seq_extract.inc

@@ -3,12 +3,13 @@
  * 
  */
 function tripal_feature_seq_extract_page() {
+  
   if ($_SESSION['tripal_feature_seq_extract']['download']){   
     $genus      = $_SESSION['tripal_feature_seq_extract']['genus'];
     $species    = $_SESSION['tripal_feature_seq_extract']['species'];
     $analysis   = $_SESSION['tripal_feature_seq_extract']['analysis'];
     $ftype      = $_SESSION['tripal_feature_seq_extract']['ftype'];
-    $fname      = $_SESSION['tripal_feature_seq_extract']['fname'];
+    $fnames     = $_SESSION['tripal_feature_seq_extract']['fnames'];
     $upstream   = $_SESSION['tripal_feature_seq_extract']['upstream'];
     $downstream = $_SESSION['tripal_feature_seq_extract']['downstream'];
     $format     = $_SESSION['tripal_feature_seq_extract']['format'];
@@ -27,7 +28,8 @@ function tripal_feature_seq_extract_page() {
     }
     
     tripal_feature_seq_extract_get_features(NULL, $genus, $species, $analysis, 
-      $ftype, $fname, $upstream, $downstream, $format, $use_parent, $aggregate, $agg_types);
+      $ftype, $fnames['items_array'], $upstream, $downstream, $format, $use_parent, $aggregate, 
+      $agg_types);
       
     return;
   }
@@ -54,25 +56,28 @@ function tripal_feature_seq_extract_page() {
 function theme_tripal_feature_seq_extract_form($form) {
   $rows = array(
     0 => array(
+      array('data' => drupal_render($form['description']), 'colspan' => 3),
+    ),
+    1 => array(
       drupal_render($form['genus']),
       drupal_render($form['species']) ,
       drupal_render($form['ftype']),
     ),
-    1 => array(
+    2 => array(
       array('data' => drupal_render($form['analysis']), 'colspan' => 3),
       //drupal_render($form['format']),    
     ),
-    2 => array(     
-      array('data' =>  drupal_render($form['fname']), 'colspan' => 2),
+    3 => array(     
+      array('data' =>  drupal_render($form['fnames']), 'colspan' => 2),
       drupal_render($form['upstream']) . drupal_render($form['downstream']) ,
     ),
-    3 => array(
+    4 => array(
       array(
         'data' =>  drupal_render($form['advanced']),
         'colspan' => 3,
       ),    
     ),
-    4 => array(
+    5 => array(
       array(
         'data' =>  drupal_render($form['retrieve_btn']),
         'colspan' => 3,
@@ -81,8 +86,7 @@ function theme_tripal_feature_seq_extract_form($form) {
   );
   $headers = array();
   $table = theme('table', $headers, $rows, array('id' => 'tripal-feature-seq-extract-form-table', 'border' => '0'));   
-  
-  $markup = drupal_render($form['description']); 
+    
   $markup .= $table;
   $form['criteria'] = array(
     '#type' => 'markup',
@@ -102,7 +106,7 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
   $dspecies    = isset($form_state['values']['species'])    ? $form_state['values']['species']    : $_SESSION['tripal_feature_seq_extract']['species'];
   $danalysis   = isset($form_state['values']['analysis'])   ? $form_state['values']['analysis']   : $_SESSION['tripal_feature_seq_extract']['analysis'];
   $dftype      = isset($form_state['values']['ftype'])      ? $form_state['values']['ftype']      : $_SESSION['tripal_feature_seq_extract']['ftype'];
-  $dfname      = isset($form_state['values']['fname'])      ? $form_state['values']['fname']      : $_SESSION['tripal_feature_seq_extract']['fname'];
+  $dfnames     = isset($form_state['values']['fnames'])     ? $form_state['values']['fnames']     : $_SESSION['tripal_feature_seq_extract']['fnames'];
   $dupstream   = isset($form_state['values']['upstream'])   ? $form_state['values']['upstream']   : $_SESSION['tripal_feature_seq_extract']['upstream'];
   $ddownstream = isset($form_state['values']['downstream']) ? $form_state['values']['downstream'] : $_SESSION['tripal_feature_seq_extract']['downstream'];
   $dformat     = isset($form_state['values']['format'])     ? $form_state['values']['format']     : 'fasta_txt';
@@ -110,6 +114,14 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
   $daggregate  = isset($form_state['values']['aggregate'])  ? $form_state['values']['aggregate']  : $_SESSION['tripal_feature_seq_extract']['aggregate'];
   $dagg_types  = isset($form_state['values']['agg_types'])  ? $form_state['values']['agg_types']  : $_SESSION['tripal_feature_seq_extract']['agg_types'];
 
+  
+  $form = array();
+  
+  // because we're using Tripal's file_upload_combo form element we
+  // need to allow the form to upload files
+  $form['#attributes']['enctype'] = 'multipart/form-data';
+  $form['#method'] = 'POST';
+    
   $form['description'] = array(
     '#type' => 'markup',
     '#value' => t('Use this form to retrieve sequences in FASTA format.')
@@ -135,8 +147,8 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
     '#multiple'      => FALSE,
     '#description'   => t('The organism\'s genus. If specified, features for all organism with this genus will be retrieved.'),
     '#ahah' => array(
-      'path'    => 'seq_extract/set_genus',
-      'wrapper' => 'tripal-feature-seq-extract-form-table',
+      'path'    => 'seq_extract/update',
+      'wrapper' => 'tripal-feature-seq-extract-form',
       'event'   => 'change',
       'method'  => 'replace',
     ), 
@@ -164,8 +176,8 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
     '#multiple'      => FALSE,
     '#description'   => t('The organism\'s species name. If specified, features for all organisms with this species will be retrieved.  Please first select a genus'),
     '#ahah' => array(
-      'path'    => 'seq_extract/set_species',
-      'wrapper' => 'tripal-feature-seq-extract-form-table',
+      'path'    => 'seq_extract/update',
+      'wrapper' => 'tripal-feature-seq-extract-form',
       'event'   => 'change',
       'method'  => 'replace',
     ), 
@@ -200,12 +212,6 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
     '#default_value' => $danalysis,    
     '#multiple'      => FALSE,
     '#description'  => t('The feature source. If specified, only features derived or part of the selected source will be retrieved.'),
-    '#ahah' => array(
-      'path'    => 'seq_extract/set_source',
-      'wrapper' => 'tripal-feature-seq-extract-form-table',
-      'event'   => 'change',
-      'method'  => 'replace',
-    ), 
   );
   
   $ftype = array();
@@ -238,11 +244,11 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
     '#description'   => t('The type of feature to retrieve (e.g. mRNA). All features that match this type will be retrieved.'),
   );
   
-  $form['fname'] = array(
+  $form['fnames'] = array(
     '#title'         => t('Feature Name'),
-    '#type'          => 'textarea',
-    '#default_value' => $dfname,
-    '#description'   => t('The names of the features to retrieve. Separate each with a space. Leave blank to retrieve all features matching other criteria.'),
+    '#type'          => 'file_upload_combo',
+    '#default_value' => $dfnames,
+    '#description'   => t('The names of the features to retrieve. Separate each with a new line or comma. Leave blank to retrieve all features matching other criteria.'),
     '#rows'          => 8
   );
   $form['upstream'] = array(
@@ -295,7 +301,7 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
   );
   $form['retrieve_btn'] = array(
     '#type' => 'submit',
-    '#value' => 'Retrieve'
+    '#value' => 'Retrieve',
   );
   return $form;
 }
@@ -303,58 +309,21 @@ function tripal_feature_seq_extract_form(&$form_state = NULL) {
 /**
  * 
  */
-function tripal_feature_seq_extract_set_genus() {
-  $status = TRUE;
-
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();   
-  $data = theme('tripal_feature_seq_extract_form', $form);  
-
-  // bind javascript events to the new objects that will be returned 
-  // so that AHAH enabled elements will work.
-  $settings = tripal_core_ahah_bind_events();
-
-  // return the updated JSON
-  drupal_json(
-    array(
-      'status'   => $status, 
-      'data'     => $data,
-      'settings' => $settings,
-    )  
-  );
-}
-/**
- * 
- */
-function tripal_feature_seq_extract_set_species() {
-  $status = TRUE;
-
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();   
-  $data = theme('tripal_feature_seq_extract_form', $form); 
-
-  // bind javascript events to the new objects that will be returned 
-  // so that AHAH enabled elements will work.
-  $settings = tripal_core_ahah_bind_events();
-
-  // return the updated JSON
-  drupal_json(
-    array(
-      'status'   => $status, 
-      'data'     => $data,
-      'settings' => $settings,
-    )  
-  );
-}
-/**
- * 
- */
-function tripal_feature_seq_extract_set_source() {
+function tripal_feature_seq_extract_form_ahah_update() {
   $status = TRUE;
 
-  // prepare and render the form
-  $form = tripal_core_ahah_prepare_form();   
-  $data = theme('tripal_feature_seq_extract_form', $form); 
+  // prepare and render the form. If no form is returned that means
+  // we got here by an AHAH call after the form has been submitted.  This
+  // is possible because results are downloaded and the page is not refreshed
+  // but the form_id goes away.  So, we need to rebuild the form in this case.
+  // otherwise, if the form already exists we just theme it.
+  $form = tripal_core_ahah_prepare_form();  
+  if ($form) { 
+    $data = theme('tripal_feature_seq_extract_form', $form);
+  }
+  else {    
+    $data = drupal_get_form('tripal_feature_seq_extract_form');    
+  }  
 
   // bind javascript events to the new objects that will be returned 
   // so that AHAH enabled elements will work.
@@ -378,7 +347,7 @@ function tripal_feature_seq_extract_form_validate($form, &$form_state) {
   $species    = $form_state['values']['species'];
   $analysis   = $form_state['values']['analysis'];
   $ftype      = $form_state['values']['ftype'];
-  $fname      = $form_state['values']['fname'];
+  $fnames      = $form_state['values']['fnames'];
   $upstream   = $form_state['values']['upstream'];
   $downstream = $form_state['values']['downstream'];
   $format     = $form_state['values']['format'];
@@ -392,7 +361,7 @@ function tripal_feature_seq_extract_form_validate($form, &$form_state) {
    if ($downstream and !preg_match('/^\d+$/', $downstream)) {
     form_set_error('downstream', 'Please enter a positive numeric value for the downstream bases');
   }
-  if (!$genus and !$species and !$ftype and !$fname) {
+  if (!$genus and !$species and !$ftype and !$fnames) {
     form_set_error('', 'Please provide a feature name, a feature type or a genus.');
   }
 }
@@ -405,7 +374,7 @@ function tripal_feature_seq_extract_form_submit($form, &$form_state) {
   $species    = $form_state['values']['species'];
   $analysis   = $form_state['values']['analysis'];
   $ftype      = $form_state['values']['ftype'];
-  $fname      = $form_state['values']['fname'];
+  $fnames     = $form_state['values']['fnames'];
   $upstream   = $form_state['values']['upstream'];
   $downstream = $form_state['values']['downstream'];
   $format     = $form_state['values']['format'];
@@ -424,7 +393,7 @@ function tripal_feature_seq_extract_form_submit($form, &$form_state) {
   $_SESSION['tripal_feature_seq_extract']['species']    = $species;
   $_SESSION['tripal_feature_seq_extract']['analysis']   = $analysis;
   $_SESSION['tripal_feature_seq_extract']['ftype']      = $ftype;
-  $_SESSION['tripal_feature_seq_extract']['fname']      = $fname;
+  $_SESSION['tripal_feature_seq_extract']['fnames']     = $fnames;
   $_SESSION['tripal_feature_seq_extract']['upstream']   = $upstream;
   $_SESSION['tripal_feature_seq_extract']['downstream'] = $downstream;
   $_SESSION['tripal_feature_seq_extract']['format']     = $format;
@@ -443,12 +412,12 @@ function tripal_feature_seq_extract_get_features($org_commonname, $genus, $speci
   $sub_features = explode(',', $child);
     
   if (!$output_format) {
-     $output_format = 'fasta_txt';
+    $output_format = 'fasta_txt';
   }
   
   if (!$type and !$feature_name and !$genus) {
-     print "Please provide a type, feature name or genus\n";
-     return;
+    print "Please provide a type, feature name or genus\n";
+     eturn;
   }
 
   // get the list of features
@@ -463,38 +432,61 @@ function tripal_feature_seq_extract_get_features($org_commonname, $genus, $speci
   }         
   $sql .= "WHERE (1=1) ";
   if ($org_commonname) {
-     $sql .= "AND O.common_name = '%s' ";
-     $vars[] = $org_commonname;
+    $sql .= "AND O.common_name = '%s' ";
+    $vars[] = $org_commonname;
   }
   if ($genus) {
-     $sql .= "AND O.genus = '%s' ";
-     $vars[] = $genus;
+    $sql .= "AND O.genus = '%s' ";
+    $vars[] = $genus;
   }
   if ($species) {
-     $sql .= "AND O.species = '%s' ";
-     $vars[] = $species;
+    $sql .= "AND O.species = '%s' ";
+    $vars[] = $species;
   }
   if ($type) {
     $sql .= "AND CVT.name = '%s' ";
     $vars[] = $type; 
   }
   if ($feature_name) {
-     $sql .= "AND F.name = '%s'";
-     $vars[] = $feature_name;
+    if (is_array($feature_name)) {
+      $sql .= "AND F.name IN (";
+      foreach ($feature_name as $i => $fname) {
+        $sql .= "'%s', ";
+        $vars[] = $fname;
+      } 
+      // remove the trailing comma and close the paren
+      $sql = substr($sql, 0, -2) . ")";
+    }
+    else {
+      $sql .= "AND F.name = '%s'";
+      $vars[] = $feature_name;
+    }
   }
   if ($analysis_name) {
-     $sql .= "AND A.name = '%s'";
-     $vars[] = $analysis_name;
+    $sql .= "AND A.name = '%s'";
+    $vars[] = $analysis_name;
   }
   $num_bases_per_line = 50;
+  $num_seqs = 0;
   $q = chado_query($sql, $vars);
+  
   while ($feature = db_fetch_object($q)) {
+    
     $feature_id = $feature->feature_id;
+    
+    // build the header for each FASTA entry
     $feature_name = "$feature->uniquename $feature->name $feature->feature_type ($feature->genus $feature->species)";
       
+    // generate the sequence
     $sequence = tripal_feature_get_formatted_sequence($feature_id, $feature_name, 
       $num_bases_per_line, $derive_from_parent, $aggregate, $output_format,
       $upstream, $downstream, $sub_features);
+    
+    // print the sequence
     print $sequence;
+    $num_seqs++;
+  }
+  if ($num_seqs == 0) {    
+    print "No Sequences Found";
   }
 }

+ 2 - 16
tripal_feature/tripal_feature.module

@@ -164,23 +164,9 @@ function tripal_feature_menu() {
     'type' => MENU_CALLBACK,
   );
   
-  $items['seq_extract/set_genus'] = array(
+  $items['seq_extract/update'] = array(
     'title' => 'Sequence Retrieval',
-    'page callback' => 'tripal_feature_seq_extract_set_genus',
-    'access arguments' => array('access chado_feature content'),
-    'type' => MENU_CALLBACK,
-  );
-  
-  $items['seq_extract/set_species'] = array(
-    'title' => 'Sequence Retrieval',
-    'page callback' => 'tripal_feature_seq_extract_set_species',
-    'access arguments' => array('access chado_feature content'),
-    'type' => MENU_CALLBACK,
-  );
-  
-  $items['seq_extract/set_source'] = array(
-    'title' => 'Sequence Retrieval',
-    'page callback' => 'tripal_feature_seq_extract_set_source',
+    'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
     'access arguments' => array('access chado_feature content'),
     'type' => MENU_CALLBACK,
   );

+ 0 - 1
tripal_views/tripal_views.module

@@ -3,7 +3,6 @@
 require_once "tripal_views.views.inc";
 
 require_once "includes/tripal_views_integration.inc";
-require_once "includes/tripal_views_form_elements.inc";
 require_once "includes/tripal_views_integration_port.inc";
 
 /**

+ 3 - 40
tripal_views/views/handlers/tripal_views_handler_filter_file_upload.inc

@@ -70,46 +70,9 @@ class tripal_views_handler_filter_file_upload extends views_handler_filter {
     $this->ensure_my_table();
     $field = "$this->table.$this->real_field";
 
-    // get the form element value
-    $value = $this->value[0];
-
-    // the form element value has sub values, so get those
-    $items =  $value['items'];
-    $file_path = $value['file_path'];
-
-    $holders = array();
-    $values = array();
-
-    // get the file upload content if one has been provided
-    if ($file_path) {
-      $fh = fopen($file_path, 'r');
-      while ($line = fgets($fh)) {
-        $items = trim($line);
-
-        // remove extra spaces and new lines
-        $items = preg_replace("/\s+,/", ",", $items);
-        $items = preg_replace("/\s+\n/", "\n", $items);
-        $items = preg_replace("/,\n/", "\n", $items);
-
-        // add the values from this line to the values[] array
-        $vals = preg_split("/[\n,]+/", $items);
-        $values = array_merge($values, $vals);
-      }
-    }
-
-    // if a file upload has not been provided then use the value in the textarea
-    if ($items) {
-
-      // remove extra spaces and new lines
-      $items = preg_replace("/\s+,/", ",", $items);
-      $items = preg_replace("/\s+\n/", "\n", $items);
-      $items = preg_replace("/,\n/", "\n", $items);
-
-      // in the event the user uploaded a file and provided items in the
-      // textbox then we need to merge these two lists
-      $vals = preg_split("/[\n,]+/", $items);
-      $values = array_merge($values, $vals);
-    }
+    // get the form element values
+    $value = $this->value[0]; 
+    $values = $value['items_array'];
 
     // iterate through all of the values and generate the corresponding
     // sprintf style holders