more error messages for loading an annotation.
[mussa.git] / alg / sequence.cpp
index 7538e4649cb2852d468e1549a53469d60a5c94b1..452f5f98f204d37bf0f85f173eb1f42fc2a07313 100644 (file)
@@ -22,6 +22,7 @@
 //                           ---------- sequence.cc -----------
 //                        ----------------------------------------
 #include <boost/filesystem/fstream.hpp>
+#include <boost/filesystem/operations.hpp>
 namespace fs = boost::filesystem;
 
 #include <boost/spirit/core.hpp>
@@ -193,6 +194,11 @@ void Sequence::load_fasta(fs::path file_path, alphabet_ref a,
       errormsg << file_path.native_file_string()
                << " did not have any fasta sequences" << std::endl;
       throw sequence_empty_file_error(errormsg.str());
+    } catch(sequence_invalid_load_error e) {
+      std::ostringstream msg;
+      msg << file_path.native_file_string();
+      msg << " " << e.what();
+      throw sequence_invalid_load_error(msg.str());
     }
   }
 }
@@ -210,6 +216,7 @@ Sequence::load_fasta(std::istream& data_file, alphabet_ref a,
 {
   std::string file_data_line;
   int header_counter = 0;
+  size_t line_counter = 0;
   bool read_seq = true;
   std::string rev_comp;
   std::string sequence_raw;
@@ -224,6 +231,7 @@ Sequence::load_fasta(std::istream& data_file, alphabet_ref a,
   while ( (!data_file.eof()) && (header_counter < seq_num) )
   {
     multiplatform_getline(data_file, file_data_line);
+    ++line_counter;
     if (file_data_line.substr(0,1) == ">")
       header_counter++;
   }
@@ -235,6 +243,7 @@ Sequence::load_fasta(std::istream& data_file, alphabet_ref a,
 
     while ( !data_file.eof() && read_seq ) {
       multiplatform_getline(data_file,file_data_line);
+      ++line_counter;
       if (file_data_line.substr(0,1) == ">")
         read_seq = false;
       else {
@@ -245,7 +254,10 @@ Sequence::load_fasta(std::istream& data_file, alphabet_ref a,
            if(alpha.exists(*line_i)) {
              sequence_raw += *line_i;
            } else {
-            throw sequence_invalid_load_error("Unrecognized characters in fasta sequence");
+            std::ostringstream msg;
+            msg << "Unrecognized characters in fasta sequence at line ";
+            msg << line_counter;
+            throw sequence_invalid_load_error(msg.str());
            }
          }
       }
@@ -302,10 +314,18 @@ void Sequence::set_filtered_sequence(const std::string &in_seq,
 void
 Sequence::load_annot(fs::path file_path, int start_index, int end_index)
 {
+  if (not fs::exists(file_path)) {
+    throw mussa_load_error("Annotation File " + file_path.string() + " was not found");
+  }
+  if (fs::is_directory(file_path)) {
+    throw mussa_load_error(file_path.string() +
+            " is a directory, please provide a file for annotations."
+          );
+  }    
   fs::fstream data_stream(file_path, std::ios::in);
   if (!data_stream)
   {
-    throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
+    throw mussa_load_error("Error loading annotation file " + file_path.string());
   }
 
   // so i should probably be passing the parse function some iterators
@@ -318,8 +338,16 @@ Sequence::load_annot(fs::path file_path, int start_index, int end_index)
     data.push_back(c);
   }
   data_stream.close();
-        
-  parse_annot(data, start_index, end_index);
+
+  try {  
+    parse_annot(data, start_index, end_index);
+  } catch(annotation_load_error e) {
+    std::ostringstream msg;
+    msg << file_path.native_file_string()
+        << " "
+        << e.what();
+    throw annotation_load_error(msg.str());
+  }
 }
 
 /* If this works, yikes, this is some brain hurting code.
@@ -418,7 +446,7 @@ Sequence::parse_annot(std::string data, int start_index, int end_index)
   std::string seq;
   std::list<annot> parsed_annots;
   std::list<Sequence> query_seqs;
-  int parsed=1;
+  int parsed=0;
 
   bool ok = spirit::parse(data.begin(), data.end(),
               (
@@ -733,6 +761,30 @@ bool Sequence::empty() const
   return (seq_count == 0) ? true : false;
 }
 
+Sequence::size_type Sequence::find_first_not_of(
+  const std::string& query, 
+  Sequence::size_type index)
+{
+  typedef std::set<std::string::value_type> sequence_set;
+  sequence_set match_set;
+  
+  for(const_iterator query_item = query.begin();
+      query_item != query.end();
+      ++query_item)
+  {
+    match_set.insert(*query_item);
+  }  
+  for(const_iterator base = begin();
+      base != end();
+      ++base)
+  {
+    if(match_set.find(*base) == match_set.end()) {
+      return base-begin();
+    } 
+  }
+  return Sequence::npos;
+}
 Sequence::size_type Sequence::start() const
 {
   if (parent)