fix ticket:83 throw an error when we don't have a fasta file
[mussa.git] / alg / sequence.cpp
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();
   }
 }