fix ticket:83 throw an error when we don't have a fasta file
authorDiane Trout <diane@caltech.edu>
Thu, 13 Apr 2006 19:34:08 +0000 (19:34 +0000)
committerDiane Trout <diane@caltech.edu>
Thu, 13 Apr 2006 19:34:08 +0000 (19:34 +0000)
If the user gives us someting that doesn't look like a fasta file throw
an error.

alg/sequence.cpp
alg/test/test_sequence.cpp
examples/seq/misformated_seq.fa [new file with mode: 0644]

index 7e7468b3f2ad3feaba2d3a1453ceb51039292ea7..9df5fd511efbdf24b3ef01cc9d91939ad27c5ef8 100644 (file)
@@ -136,28 +136,30 @@ Sequence::load_fasta(fs::path file_path, int seq_num,
         header_counter++;
     }
 
-    header = file_data_line.substr(1);
+    if (header_counter > 0) {
+      header = file_data_line.substr(1);
 
-    sequence_raw = "";
+      sequence_raw = "";
 
-    while ( !data_file.eof() && read_seq )
-    {
-      getline(data_file,file_data_line);
-      if (file_data_line.substr(0,1) == ">")
-        read_seq = false;
-      else sequence_raw += file_data_line;
-    }
-
-    data_file.close();
+      while ( !data_file.eof() && read_seq ) {
+        getline(data_file,file_data_line);
+        if (file_data_line.substr(0,1) == ">")
+          read_seq = false;
+        else sequence_raw += file_data_line;
+      }
 
-    // Lastly, if subselection of the sequence was specified we keep cut out
-    // and only keep that part
-    // end_index = 0 means no end was specified, so cut to the end 
-    if (end_index == 0)
-      end_index = sequence_raw.size();
+      // Lastly, if subselection of the sequence was specified we keep cut out
+      // and only keep that part
+      // end_index = 0 means no end was specified, so cut to the end 
+      if (end_index == 0)
+        end_index = sequence_raw.size();
 
-    // sequence filtering for upcasing agctn and convert non AGCTN to N
-    set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
+      // sequence filtering for upcasing agctn and convert non AGCTN to N
+      set_filtered_sequence(sequence_raw, start_index, end_index-start_index);
+    } else {
+      throw mussa_load_error("%s did not have a fasta header");
+    }
+    data_file.close();
   }
 }
 
index 1a4e9ac738598af56be6614975170676656a1714..09c621e937874d673a2c66dcae3df69f817ae59e 100644 (file)
@@ -63,6 +63,16 @@ BOOST_AUTO_TEST_CASE( sequence_load )
                                     "5' flank");
 }
 
+// ticket:83 when you try to load a sequence from a file that doesn't
+// have fasta headers it crashes. 
+BOOST_AUTO_TEST_CASE( sequence_past_end ) 
+{
+  fs::path seq_path(fs::path(EXAMPLE_DIR)/ "seq" );
+  seq_path /=  "misformated_seq.fa";
+  Sequence s;
+  BOOST_CHECK_THROW( s.load_fasta(seq_path), mussa_load_error );
+}
+
 BOOST_AUTO_TEST_CASE ( sequence_empty )
 {
   Sequence s;
diff --git a/examples/seq/misformated_seq.fa b/examples/seq/misformated_seq.fa
new file mode 100644 (file)
index 0000000..7cddfd3
--- /dev/null
@@ -0,0 +1,6 @@
+GGATCCTTCCTCCTTGGCCTCCCAAAGTGCTGGGATTACAGGTGTGAGCCACTGCACCTGGCCTATTACC
+CTTCTCAGGCTCTGGAGTCCATCCTTCTGCTCTGTCTCCCTCAGTTCAATTGTTTTTTGTTTTTTGTTTT
+TTTTTTAGACACAGTCTCGCTCTGTCACCAAGGCTGGAGTGCAGCAGTGCGATCACAGCTCACCGCAGCC
+TCACCTCCCAGGCTCAAGTGATCCTCCCATCTCGGCCTCTGAGTAGCTGAGACTATAGGTGTGTCCACAT
+GTCCGGCTAATTTTTGTATTTTTAGTAGAGACAGGGTTTCACCGCGTTGGCCAGGGTGGTCTTGAACTCC
+TGAGCTCAAGCAATCCTCCTGCCTCAGCCTCCTTGTTTTGATTTTTAGATCCCACAAATAACTTGTGATG