make mupa file loading eol-style insensitive
[mussa.git] / alg / mussa.cpp
index 82b1d4a6585e49c1a946e9093413b0be357ab570..9a5935e19947cbd68e28e7c7a3f88b267816030f 100644 (file)
@@ -21,9 +21,11 @@ namespace fs = boost::filesystem;
 #include <sstream>
 
 #include "mussa_exceptions.hpp"
-#include "alg/flp.hpp"
-#include "alg/mussa.hpp"
-#include "alg/motif_parser.hpp"
+
+#include "flp.hpp"
+#include "io.hpp"
+#include "mussa.hpp"
+#include "motif_parser.hpp"
 
 using namespace std;
 
@@ -350,10 +352,35 @@ void Mussa::load_sequence(fs::path seq_file, fs::path annot_file,
   set_dirty(true);
 }
 
+void Mussa::load_mupa_file(std::string para_file_path) {
+  load_mupa_file(boost::filesystem::path(para_file_path));
+}
+
 void
 Mussa::load_mupa_file(fs::path para_file_path)
 {
-  fs::ifstream para_file;
+  if (not fs::exists(para_file_path))
+  {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
+  } else if (fs::is_directory(para_file_path)) {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " is a directory.");
+  } else if (fs::is_empty(para_file_path)) {
+    throw mussa_load_error("Config File: " + para_file_path.string() + " is empty");
+  } else  {
+    // what directory is the mupa file in?
+    fs::path file_path_base( para_file_path.branch_path()) ;
+
+    fs::ifstream para_file;
+    para_file.open(para_file_path, ios::in);
+    
+    load_mupa_stream(para_file, file_path_base);
+    para_file.close();   
+  }
+}
+
+void
+Mussa::load_mupa_stream(std::istream& para_file, fs::path& file_path_base)
+{
   string file_data_line;
   string param, value; 
   fs::path annot_file;
@@ -367,101 +394,84 @@ Mussa::load_mupa_file(fs::path para_file_path)
   // initialize values
   clear();
 
-  // if file was opened, read the parameter values
-  if (not fs::exists(para_file_path))
+  // setup loop by getting file's first line
+  getline(para_file, file_data_line);
+  split_index = file_data_line.find(" ");
+  param = file_data_line.substr(0,split_index);
+  value = file_data_line.substr(split_index+1);
+  
+  while (para_file)
   {
-    throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
-  } else if (fs::is_directory(para_file_path)) {
-    throw mussa_load_error("Config File: " + para_file_path.string() + " is a directory.");
-  } else if (fs::is_empty(para_file_path)) {
-    throw mussa_load_error("Config File: " + para_file_path.string() + " is empty");
-  } else  {
-    para_file.open(para_file_path, ios::in);
-
-    // what directory is the mupa file in?
-    fs::path file_path_base = para_file_path.branch_path();
-
-    // setup loop by getting file's first line
-    getline(para_file,file_data_line);
-    split_index = file_data_line.find(" ");
-    param = file_data_line.substr(0,split_index);
-    value = file_data_line.substr(split_index+1);
-    
-    while (para_file)
+    did_seq = false;
+    if (param == "ANA_NAME")
+      analysis_name = value;
+    else if (param == "APPEND_WIN")
+      win_append = true;
+    else if (param == "APPEND_THRES")
+      thres_append = true;
+    else if (param == "SEQUENCE_NUM")
+      ; // ignore sequence_num now
+    else if (param == "WINDOW")
+      window = atoi(value.c_str());
+    else if (param == "THRESHOLD")
+      threshold = atoi(value.c_str());
+    else if (param == "SEQUENCE")
     {
-      did_seq = false;
-      if (param == "ANA_NAME")
-        analysis_name = value;
-      else if (param == "APPEND_WIN")
-        win_append = true;
-      else if (param == "APPEND_THRES")
-        thres_append = true;
-      else if (param == "SEQUENCE_NUM")
-        ; // ignore sequence_num now
-      else if (param == "WINDOW")
-        window = atoi(value.c_str());
-      else if (param == "THRESHOLD")
-        threshold = atoi(value.c_str());
-      else if (param == "SEQUENCE")
-      {
-        fs::path seq_file = file_path_base / value;
-        //cout << "seq_file_name " << seq_files.back() << endl;
-        fasta_index = 1;
-        annot_file = "";
-        sub_seq_start = 0;
-        sub_seq_end = 0;
-        seq_params = true;
-
-        while (para_file && seq_params)
-        {
-          getline(para_file,file_data_line);
-          split_index = file_data_line.find(" ");
-          param = file_data_line.substr(0,split_index);
-          value = file_data_line.substr(split_index+1);
-
-          if (param == "FASTA_INDEX")
-            fasta_index = atoi(value.c_str());
-          else if (param == "ANNOTATION")
-            annot_file = file_path_base / value;
-          else if (param == "SEQ_START")
-            sub_seq_start = atoi(value.c_str());
-          else if (param == "SEQ_END")
-          {
-            sub_seq_end = atoi(value.c_str());
-          }
-          //ignore empty lines or that start with '#'
-          else if ((param == "") || (param == "#")) {}
-          else seq_params = false;
-        }
-        load_sequence(seq_file, annot_file, fasta_index, sub_seq_start, 
-                      sub_seq_end);
-        did_seq = true;
-      }
-      //ignore empty lines or that start with '#'
-      else if ((param == "") || (param == "#")) {}
-      else
-      {
-        clog << "Illegal/misplaced mussa parameter in file\n";
-        clog << param << "\n";
-      }
-
-      if (!did_seq)
+      fs::path seq_file = file_path_base / value;
+      //cout << "seq_file_name " << seq_files.back() << endl;
+      fasta_index = 1;
+      annot_file = "";
+      sub_seq_start = 0;
+      sub_seq_end = 0;
+      seq_params = true;
+
+      while (para_file && seq_params)
       {
-        getline(para_file,file_data_line);
+        multiplatform_getline(para_file,file_data_line);
         split_index = file_data_line.find(" ");
         param = file_data_line.substr(0,split_index);
         value = file_data_line.substr(split_index+1);
-        did_seq = false;
+
+        if (param == "FASTA_INDEX")
+          fasta_index = atoi(value.c_str());
+        else if (param == "ANNOTATION")
+          annot_file = file_path_base / value;
+        else if (param == "SEQ_START")
+          sub_seq_start = atoi(value.c_str());
+        else if (param == "SEQ_END")
+        {
+          sub_seq_end = atoi(value.c_str());
+        }
+        //ignore empty lines or that start with '#'
+        else if ((param == "") || (param == "#")) {
+          // pass 
+        } else {
+          seq_params = false;
+        }
       }
+      load_sequence(seq_file, annot_file, fasta_index, sub_seq_start, 
+                    sub_seq_end);
+      did_seq = true;
+    }
+    //ignore empty lines or that start with '#'
+    else if ((param == "") || (param == "#")) {}
+    else
+    {
+      clog << "Illegal/misplaced mussa parameter in file\n";
+      clog << param << "\n";
     }
 
-    para_file.close();
-
-    soft_thres = threshold;
-    //cout << "nway mupa: analysis_name = " << analysis_name 
-    //     << " window = " << window 
-    //     << " threshold = " << threshold << endl;
+    if (!did_seq)
+    {
+      multiplatform_getline(para_file,file_data_line);
+      split_index = file_data_line.find(" ");
+      param = file_data_line.substr(0,split_index);
+      value = file_data_line.substr(split_index+1);
+      did_seq = false;
+    }
   }
+
+  soft_thres = threshold;
   // no file was loaded, signal error
   set_dirty(true);
 }