浏览代码

Merge pull request #20 from LegumeFederation/fasta_validation

Changes to FASTA validation
ekcannon 7 年之前
父节点
当前提交
d83d5881a9
共有 2 个文件被更改,包括 26 次插入26 次删除
  1. 13 17
      api/blast_ui.api.inc
  2. 13 9
      includes/blast_ui.form_per_program.inc

+ 13 - 17
api/blast_ui.api.inc

@@ -327,25 +327,21 @@ function run_BLAST_tripal_job($program, $query, $database, $output_filestub, $op
  *
  *
  */
  */
 function validate_fasta_sequence($type, $sequence) {
 function validate_fasta_sequence($type, $sequence) {
-
-  if ($type == 'nucleotide') {
-    $fastaIdRegEx = '/^>.*(\\n|\\r)/';
-    $fastaSeqRegEx = '/[^ATCGNUKMBVSWDYRHatcgnukmbvswdyrh\n\r]/'; //Includes IUPAC codes.
-    if ( preg_match($fastaSeqRegEx,$sequence) && !(preg_match($fastaIdRegEx,$sequence)) ) {
-      return TRUE;
-    } else {
-      return FALSE;
-    }
-  } elseif ($type == 'protein') {
-    $fastaIdRegEx = '/^>.*(\\n|\\r)/';
-    $fastaSeqRegEx = '/[^ABCDEFGHIKLMNPQRSTUVWYZXabcdefghiklmnpqrstuvwyzx\*\-\n\r]/';
-    if ( preg_match($fastaSeqRegEx,$sequence) && !(preg_match($fastaIdRegEx,$sequence)) ) {
-      return TRUE;
-    } else {
-      return FALSE;
+  //Includes IUPAC codes.
+  $fastaSeqRegEx = ($type == 'nucleotide') 
+                   ? '/^[ATCGNUKMBVSWDYRHatcgnukmbvswdyrh\s\n\r]*$/' 
+                   : '/^[ABCDEFGHIKLMNPQRSTUVWYZXabcdefghiklmnpqrstuvwyzx\*\-\s\n\r]*$/';
+  $defRegEx      = '/^>.*(\\n|\\r)(.*)$/sm';
+  if (preg_match($defRegEx, $sequence, $matches)) {
+    if (isset($matches[2]) && $matches[2] != '' && preg_match($fastaSeqRegEx, $matches[2])) {
+      return true;
     }
     }
   }
   }
-  return FALSE;
+  else if ($sequence != '' && preg_match($defRegEx, $sequence)) {
+    return true;
+  }
+  
+  return false;
 }
 }
 
 
 /**
 /**

+ 13 - 9
includes/blast_ui.form_per_program.inc

@@ -43,7 +43,7 @@ function blast_ui_per_blast_program_form($form, $form_state) {
     if (!isset($prev_blast->blastdb->nid)) {
     if (!isset($prev_blast->blastdb->nid)) {
       drupal_set_message('You will need to re-upload your <em>Search Target</em> database.','warning');
       drupal_set_message('You will need to re-upload your <em>Search Target</em> database.','warning');
     }
     }
-    // Andi f they didn't upload a target then set a default for the select list.
+    // And if they didn't upload a target then set a default for the select list.
     else {
     else {
       $defaults['SELECT_DB'] = $prev_blast->blastdb->nid;
       $defaults['SELECT_DB'] = $prev_blast->blastdb->nid;
     }
     }
@@ -223,6 +223,7 @@ function blast_ui_per_blast_program_form($form, $form_state) {
     '#type' => 'select',
     '#type' => 'select',
     '#title' => t('%type BLAST Databases:', array('%type' => ucfirst($db_type))),
     '#title' => t('%type BLAST Databases:', array('%type' => ucfirst($db_type))),
     '#options' => $options,
     '#options' => $options,
+    '#empty_option' => t('Select a Dataset'),
     '#default_value' => $defaults['SELECT_DB'],
     '#default_value' => $defaults['SELECT_DB'],
   );
   );
 
 
@@ -312,10 +313,9 @@ function blast_ui_per_blast_program_form_validate($form, &$form_state) {
   // Check if there was a query sequence entered in the texfield.
   // Check if there was a query sequence entered in the texfield.
   elseif (!empty($form_state['input']['FASTA'])) {
   elseif (!empty($form_state['input']['FASTA'])) {
     // Check to ensure that the query sequence entered is valid FASTA.
     // Check to ensure that the query sequence entered is valid FASTA.
-    if (validate_fasta_sequence($type, $form_state['input']['FASTA'])){
-      form_set_error('query', t('You need to provide a valid FASTA sequence '
-      . 'for the query. It should contain a FASTA header/definition line '
-      . 'followed by %molecule-type. For more information see the '
+    if (!validate_fasta_sequence($type, $form_state['input']['FASTA'])){
+      form_set_error('query', t('You need to provide a valid %molecule-type FASTA sequence '
+      . 'for the query. For more information see the '
       . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
       . '<a href="@url" target="_blank">NCBI FASTA Specification</a>.',
         array(
         array(
           '%molecule-type' => $molecule_type,
           '%molecule-type' => $molecule_type,
@@ -441,10 +441,14 @@ function blast_ui_per_blast_program_form_submit($form, &$form_state) {
     // Check that the BLAST database was made correctly.
     // Check that the BLAST database was made correctly.
     $result = implode('<br />', $result);
     $result = implode('<br />', $result);
     if (preg_match('/Error/', $result)) {
     if (preg_match('/Error/', $result)) {
-      drupal_set_message('Unable to generate a BLAST database from your uploaded
-FASTA sequence. Please check that your file is a valid FASTA file and that if
-your sequence headers include pipes (i.e.: | ) they adhere to '
-. l('NCBI standards.', 'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', array('attributes' => array('target' => '_blank'))),
+      drupal_set_message(t('Unable to generate a BLAST database from your uploaded FASTA '
+                          .'sequence. Please check that your file is a valid FASTA file '
+                          .'and that if your sequence headers include pipes (i.e.: | ) '
+                          .' they adhere to ') 
+                          .l('NCBI standards.', 
+                             'http://www.ncbi.nlm.nih.gov/books/NBK21097/table/A632/?report=objectonly', 
+                             array('attributes' => array('target' => '_blank'))
+                          ),
         'error'
         'error'
       );
       );