switch to using boost::filesystem
[mussa.git] / alg / mussa.cpp
index e713f52cf7130ea76f40b72a464416d855f6b1e5..e432b58e8392cea350dad20cb3da9908eb61e534 100644 (file)
 //                        ----------------------------------------
 //                          ---------- mussa_class.cc -----------
 //                        ----------------------------------------
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/fstream.hpp>
+namespace fs = boost::filesystem;
 
-#include <fstream>
 #include <iostream>
 #include <sstream>
 
@@ -166,24 +168,24 @@ Mussa::sequences() const
   return the_seqs;
 }
 
-void Mussa::load_sequence(string seq_file, string annot_file, int fasta_index
-                          int sub_seq_start, int sub_seq_end)
+void Mussa::load_sequence(fs::path seq_file, fs::path annot_file
+                          int fasta_index, int sub_seq_start, int sub_seq_end)
 {
   Sequence aseq;
   aseq.load_fasta(seq_file, fasta_index, sub_seq_start, sub_seq_end);
-  if (annot_file.size() > 0) {
+  if ( not annot_file.empty() ) {
     aseq.load_annot(annot_file, sub_seq_start, sub_seq_end);
   }
   the_seqs.push_back(aseq);
 }
 
 void
-Mussa::load_mupa_file(string para_file_path)
+Mussa::load_mupa_file(fs::path para_file_path)
 {
-  string file_path_base;
-  ifstream para_file;
+  fs::ifstream para_file;
   string file_data_line;
-  string param, value, annot_file;
+  string param, value; 
+  fs::path annot_file;
   int split_index, fasta_index;
   int sub_seq_start, sub_seq_end;
   bool seq_params, did_seq;
@@ -194,24 +196,13 @@ Mussa::load_mupa_file(string para_file_path)
   // initialize values
   clear();
 
-  para_file.open(para_file_path.c_str(), ios::in);
-
   // if file was opened, read the parameter values
-  if (para_file)
+  if (fs::exists(para_file_path))
   {
-    // need to find the path to the .mupa file
-    parsing_path = true;
-    dir_index = 0;
-    while (parsing_path)
-    {
-      new_index = (para_file_path.substr(dir_index)).find("/");
-      if (new_index != string::npos)
-        dir_index += new_index + 1;
-      else
-        parsing_path = false;
-    }
+    para_file.open(para_file_path, ios::in);
 
-    file_path_base = para_file_path.substr(0,dir_index);
+    // 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);
@@ -236,7 +227,7 @@ Mussa::load_mupa_file(string para_file_path)
         threshold = atoi(value.c_str());
       else if (param == "SEQUENCE")
       {
-        string seq_file = file_path_base + value;
+        fs::path seq_file = file_path_base / value;
         //cout << "seq_file_name " << seq_files.back() << endl;
         fasta_index = 1;
         annot_file = "";
@@ -254,7 +245,7 @@ Mussa::load_mupa_file(string para_file_path)
           if (param == "FASTA_INDEX")
             fasta_index = atoi(value.c_str());
           else if (param == "ANNOTATION")
-            annot_file = file_path_base + value;
+            annot_file = file_path_base / value;
           else if (param == "SEQ_START")
             sub_seq_start = atoi(value.c_str());
           else if (param == "SEQ_END")
@@ -297,7 +288,7 @@ Mussa::load_mupa_file(string para_file_path)
   // no file was loaded, signal error
   else
   {
-    throw mussa_load_error("Config File: " + para_file_path + " not found");
+    throw mussa_load_error("Config File: " + para_file_path.string() + " not found");
   }
 }
 
@@ -424,93 +415,74 @@ Mussa::nway()
 void
 Mussa::save()
 {
-  string save_name, save_path, create_dir_cmd, flp_filepath;
-  fstream save_file;
+  string save_name;
+  fs::path flp_filepath;
+  fs::fstream save_file;
   ostringstream append_info;
   int dir_create_status;
 
+  if (not analysis_name.empty()) {
+    // not sure why, but gotta close file each time since can't pass 
+    // file streams
+    save_name = analysis_name;
 
-  // not sure why, but gotta close file each time since can't pass file streams
-
-  save_name = analysis_name;
-
-  // gotta do bit with adding win & thres if to be appended
-  if (win_append)
-  {
-    append_info.str("");
-    append_info <<  "_w" << window;
-    save_name += append_info.str();
-  }
-
-  if (thres_append)
-  {
-    append_info.str("");
-    append_info <<  "_t" << threshold;
-    save_name += append_info.str();
-  }
+    // gotta do bit with adding win & thres if to be appended
+    if (win_append)
+    {
+      append_info.str("");
+      append_info <<  "_w" << window;
+      save_name += append_info.str();
+    }
 
-//#include <stdlib.h>
-  // ******* use appropriate for os ------- 1 of 4
-  // the additions for osX make it more sane where it saves the analysis
-  // will come up with a cleaner sol'n later...
-  create_dir_cmd = "mkdir " + save_name;       //linux
-  //create_dir_cmd = "mkdir " + file_path_base + save_name;  //osX
+    if (thres_append)
+    {
+      append_info.str("");
+      append_info <<  "_t" << threshold;
+      save_name += append_info.str();
+    }
+    fs::path save_path( save_name);
 
-  dir_create_status = system( (const char*) create_dir_cmd.c_str());
-  //cout << "action: " << dir_create_status << endl;
+    if (not fs::exists(save_path)) {
+      fs::create_directory(save_path);
+    }
+    // save sequence and annots to a special mussa file
+    save_file.open(save_path / (save_name+".museq"), ios::out);
+    save_file << "<Mussa_Sequence>" << endl;
 
-  // save sequence and annots to a special mussa file
+    for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
+    {
+      the_seqs[i].save(save_file);
+    }
 
-  // ******** use appropriate for OS ---------- 2 of 4
-  save_path = save_name + "/" + save_name + ".museq";   //linux
-  //save_path = file_path_base + save_name + "/" + save_name + ".museq"; //osX
+    save_file << "</Mussa_Sequence>" << endl;
+    save_file.close();
 
-  save_file.open(save_path.c_str(), ios::out);
-  save_file << "<Mussa_Sequence>" << endl;
-  //save_file.close();
+    // save nway paths to its mussa save file
+    the_paths.save(save_path / (save_name + ".muway"));
 
-  for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
-  {
-    the_seqs[i].save(save_file);
+    for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
+      for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
+      {
+        append_info.str("");
+        append_info <<  "_sp_" << i << "v" << i2;
+        all_comps[i][i2].save(save_path/(save_name+append_info.str()+".flp"));
+      }
   }
-
-  //save_file.open(save_path.c_str(), ios::app);
-  save_file << "</Mussa_Sequence>" << endl;
-  save_file.close();
-
-  // save nway paths to its mussa save file
-
-  // ******** use appropriate for OS -------- 3 of 4
-  save_path = save_name + "/" + save_name + ".muway";  //linux
-  //save_path = file_path_base + save_name + "/" + save_name + ".muway"; //os X
-  the_paths.save(save_path);
-
-  for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
-    for(vector<Sequence>::size_type i2 = i+1; i2 < the_seqs.size(); i2++)
-    {
-      append_info.str("");
-      append_info <<  "_sp_" << i << "v" << i2;
-      // ******** use appropriate for OS --------- 4 of 4
-      //linux
-      save_path = save_name + "/" + save_name + append_info.str() + ".flp";
-      //osX
-      //save_path = file_path_base + save_name + "/" + save_name + append_info.str() + ".flp";
-      all_comps[i][i2].save(save_path);
-    }
 }
 
 void
-Mussa::save_muway(string save_path)
+Mussa::save_muway(fs::path save_path)
 {
   the_paths.save(save_path);
 }
 
 void
-Mussa::load(string ana_file)
+Mussa::load(fs::path ana_file)
 {
   int i, i2;
-  string::size_type start_index, end_index;
-  string file_path_base, a_file_path, ana_path;
+  fs::path file_path_base;
+  fs::path a_file_path; 
+  fs::path ana_path(ana_file);
   bool parsing_path;
   Sequence tmp_seq;
   string err_msg;
@@ -518,27 +490,12 @@ Mussa::load(string ana_file)
   vector<FLPs> empty_FLP_vector;
   FLPs dummy_comp;
 
-  //cout << "ana_file name " << ana_file << endl;
-  ana_path = ana_file;
-  parsing_path = true;
-  end_index = ana_path.size()-1;
-  if (ana_path[end_index] == '/') { 
-    --end_index;
-  }
-  start_index = ana_path.rfind('/', end_index);
-  if (start_index == string::npos) {
-    // no / to be found
-    start_index = 0;
-  } else {
-    // skip the / we found
-    ++start_index;
-  }
-  analysis_name = ana_path.substr(start_index, end_index-start_index+1);
+  //cout << "ana_file name " << ana_file.string() << endl;
+  analysis_name = ana_path.leaf();
   //cout << " ana_name " << analysis_name << endl;
-  file_path_base =  ana_path.substr(0, start_index) + analysis_name 
-                    + "/" + analysis_name;
-  a_file_path = file_path_base + ".muway";
-  //cout << " loading museq: " << a_file_path << endl;
+  file_path_base =  ana_path.branch_path() / analysis_name;
+  a_file_path = file_path_base / (analysis_name + ".muway");
+  //cout << " loading museq: " << a_file_path.string() << endl;
   the_paths.load(a_file_path);
   // perhaps this could be more elegent, but at least this'll let
   // us know what our threshold and window sizes were when we load a muway
@@ -548,13 +505,13 @@ Mussa::load(string ana_file)
 
   int seq_num = the_paths.sequence_count();
 
-  a_file_path = file_path_base + ".museq";
+  a_file_path = file_path_base / (analysis_name + ".museq");
 
   // this is a bit of a hack due to C++ not acting like it should with files
   for (i = 1; i <= seq_num; i++)
   {
     tmp_seq.clear();
-    //cout << "mussa_class: loading museq frag... " << a_file_path << endl;
+    //cout << "mussa_class: loading museq frag... " << a_file_path.string() << endl;
     tmp_seq.load_museq(a_file_path, i);
     the_seqs.push_back(tmp_seq);
   }
@@ -572,9 +529,10 @@ Mussa::load(string ana_file)
     for(i2 = i+1; i2 < seq_num; i2++)
     {
       append_info.str("");
-      append_info <<  "_sp_" << i << "v" << i2;
+      append_info << analysis_name <<  "_sp_" << i << "v" << i2 << ".flp";
       //cout << append_info.str() << endl;
-      a_file_path = file_path_base + append_info.str() + ".flp";
+      a_file_path = file_path_base / append_info.str();
+      //cout << "path " << a_file_path.string() << endl;
       all_comps[i][i2].load(a_file_path);
       //cout << "real size = " << all_comps[i][i2].size() << endl;
     }
@@ -585,9 +543,9 @@ Mussa::load(string ana_file)
 void
 Mussa::save_old()
 {
-  fstream save_file;
+  fs::fstream save_file;
 
-  save_file.open(analysis_name.c_str(), ios::out);
+  save_file.open(analysis_name, ios::out);
 
   for(vector<Sequence>::size_type i = 0; i < the_seqs.size(); i++)
     save_file << the_seqs[i].get_seq() << endl;
@@ -720,10 +678,10 @@ void Mussa::load_motifs(std::istream &in)
   update_sequences_motifs();
 }
 
-void Mussa::load_motifs(string filename)
+void Mussa::load_motifs(fs::path filename)
 {
-  ifstream f;
-  f.open(filename.c_str(), ifstream::in);
+  fs::ifstream f;
+  f.open(filename, ifstream::in);
   load_motifs(f);
 }