switch to using boost::filesystem
authorDiane Trout <diane@caltech.edu>
Fri, 7 Apr 2006 02:10:30 +0000 (02:10 +0000)
committerDiane Trout <diane@caltech.edu>
Fri, 7 Apr 2006 02:10:30 +0000 (02:10 +0000)
I changed the loading code to use boost::filesystem, and also updated the
build system to hardcode the location of the example data in the unittest
program.

The result of this is I can now use easier filesystem handling code
and my unittests will work even when we build the app out of source

15 files changed:
alg/flp.cpp
alg/flp.hpp
alg/mussa.cpp
alg/mussa.hpp
alg/nway_paths.cpp
alg/nway_paths.hpp
alg/sequence.cpp
alg/sequence.hpp
alg/test/CMakeLists.txt
alg/test/test_flp.cpp
alg/test/test_mussa.cpp
alg/test/test_nway.cpp
alg/test/test_sequence.cpp
qui/CMakeLists.txt
qui/mussagl.cpp

index 3076e7b437b24526010adff9262d53d470f388d4..88ff209a9eeec64470ac7ce450c97e06a0484674 100644 (file)
 //                            ---------- flp.cc  -----------
 //                        ----------------------------------------
 
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/fstream.hpp>
+namespace fs = boost::filesystem;
+
 #include "alg/flp.hpp"
 
-#include <fstream>
 #include <iostream>
 #include <string>
 #include <stdexcept>
 #include <cassert>
 
+#include "mussa_exceptions.hpp"
 using namespace std;
 
 bool operator==(const FLPs::match& a, const FLPs::match& b)
@@ -168,13 +172,13 @@ FLPs::thres_matches(int index, int thres) const
 }
 
 void
-FLPs::save(string save_file_path)
+FLPs::save(fs::path save_file_path)
 {
   if (all_matches == 0) 
     throw runtime_error("please call FLPs.seqcomp first");
 
-  fstream save_file;
-  save_file.open(save_file_path.c_str(), ios::out);
+  fs::fstream save_file;
+  save_file.open(save_file_path, ios::out);
 
   save_file << "<Seqcomp win=" << window_size
             << " thres=" << hard_threshold << endl;
@@ -201,9 +205,9 @@ FLPs::save(string save_file_path)
 }
 
 void
-FLPs::load(string file_path)
+FLPs::load(fs::path file_path)
 {
-  fstream data_file;
+  fs::fstream data_file;
   string file_data, file_data_line, pair_data, index_data, score_data;
   match a_match;
   string::size_type split_index, comma_index;
@@ -212,57 +216,61 @@ FLPs::load(string file_path)
   // initialize our all_matches pointer
   alloc_matches();
 
-
-  data_file.open(file_path.c_str(), ios::in);
-
-  getline(data_file,file_data_line);
-  // parse seqcomp open tag and parameters
-  // eg <Seqcomp type=mussa win=30 thres=21>
-  // if parse successful...
-  tag_open = true;
-
-  while ((!data_file.eof()) && tag_open)
-  {
-    // intialize list to empty
-    a_match_list.clear();
+  if (fs::exists(file_path)) {
+    data_file.open(file_path, ios::in);
 
     getline(data_file,file_data_line);
-    if (file_data_line == "</Seqcomp>")
-    {
-      tag_open = false;
-    }
-    // parse line of matches
-    else if (file_data_line == "")
-    {
-      //cout << "empty line\n";
-      all_matches->push_back(a_match_list); 
-    }
-    else
+    // parse seqcomp open tag and parameters
+    // eg <Seqcomp type=mussa win=30 thres=21>
+    // if parse successful...
+    tag_open = true;
+
+    while ((data_file.good()) && tag_open)
     {
-      split_index = file_data_line.find(" ");
-      
-      while (split_index != string::npos)
-      {
-        pair_data = file_data_line.substr(0,split_index); 
-        file_data_line = file_data_line.substr(split_index+1);
-        //cout << "pair_data = " << pair_data << "...";
-        // parse out the 2 pieces of data, index and score of pair match
-        comma_index = pair_data.find(",");
-        index_data = pair_data.substr(0, comma_index);
-        a_match.index = atoi(index_data.c_str() );
-        score_data = pair_data.substr(comma_index+1); 
-        a_match.score = atoi(score_data.c_str() );
-        //cout << a_match.index << "," << a_match.score << " ";
-
-        a_match_list.push_back(a_match);
+      // intialize list to empty
+      a_match_list.clear();
 
+      getline(data_file,file_data_line);
+      if (file_data_line == "</Seqcomp>")
+      {
+        tag_open = false;
+      }
+      // parse line of matches
+      else if (file_data_line == "")
+      {
+        //cout << "empty line\n";
+        all_matches->push_back(a_match_list); 
+      }
+      else
+      {
         split_index = file_data_line.find(" ");
+      
+        while (split_index != string::npos)
+        {
+          pair_data = file_data_line.substr(0,split_index); 
+          file_data_line = file_data_line.substr(split_index+1);
+          //cout << "pair_data = " << pair_data << "...";
+          // parse out the 2 pieces of data, index and score of pair match
+          comma_index = pair_data.find(",");
+          index_data = pair_data.substr(0, comma_index);
+          a_match.index = atoi(index_data.c_str() );
+          score_data = pair_data.substr(comma_index+1); 
+          a_match.score = atoi(score_data.c_str() );
+          //cout << a_match.index << "," << a_match.score << " ";
+
+          a_match_list.push_back(a_match);
+
+          split_index = file_data_line.find(" ");
+        }
+        all_matches->push_back(a_match_list);
+        //cout << all_matches->size() << "\n";
       }
-      all_matches->push_back(a_match_list);
-      //cout << all_matches->size() << "\n";
     }
+    //cout << "windows in flp = " << all_matches->size() << endl;
+    data_file.close();
+  } else {
+    throw mussa_load_error(file_path.string() + "was not found");
   }
-  //cout << "windows in flp = " << all_matches->size() << endl;
-  data_file.close();
+  
 }
 
index ed9c02bce36a360ee7439c586b811910e88c3798..0f5f892234ae4fac78093db0885bf8cad115191a 100644 (file)
@@ -14,6 +14,8 @@
 //                            ---------- flp.hh  -----------
 //                        ----------------------------------------
 
+#include <boost/filesystem/path.hpp>
+
 #include <list>
 #include <string>
 #include <vector>
@@ -58,9 +60,9 @@ class FLPs
      */
     int size() const;
     //! Save all the FLPs to save_file_path
-    void save(std::string save_file_path);
+    void save(boost::filesystem::path save_file_path);
     //! Load a vector of a lists of FLPs
-    void load(std::string file_path);
+    void load(boost::filesystem::path file_path);
   private:
     //! the number of base pairs in the sliding window
     int window_size;
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);
 }
 
index b4a6e26d4638d5cbed7dc1f25c29ffc7d9a3ad41..675658d4a65ad55a2b2f71076a48f363afe87cdb 100644 (file)
@@ -13,6 +13,7 @@
 //                        ----------------------------------------
 //                          ---------- mussa_class.hh -----------
 //                        ----------------------------------------
+#include <boost/filesystem/path.hpp>
 
 #include <list>
 #include <string>
@@ -37,15 +38,16 @@ class Mussa
     Mussa(const Mussa &);
 
     void save();
-    void save_muway(std::string save_path);
+    //! save the nway comparison
+    void save_muway(boost::filesystem::path save_path);
     //! load a saved analysis directory
-    void load(std::string ana_path);
+    void load(boost::filesystem::path ana_path);
 
     //! clear parameters and initialize data lists
     void clear();
 
-    // set parameters from a file - 'mupa' ~ mussa parameters
-    void load_mupa_file(std::string para_file_path);
+    //! set parameters from a file - 'mupa' ~ mussa parameters
+    void load_mupa_file(boost::filesystem::path para_file_path);
 
     // set parameters individually (eg from user input into gui classes)
     //! set analysis name
@@ -105,7 +107,8 @@ class Mussa
      *  \param[in] sub_seq_end ending slice index to select a subsequence
      *             use 0 to go to the end.
      */
-    void load_sequence(std::string seq_file, std::string annot_file, 
+    void load_sequence(boost::filesystem::path seq_file, 
+                       boost::filesystem::path annot_file, 
                        int fasta_index, int sub_seq_start=0, int sub_seq_end=0);
     //! allow examining the sequences we have loaded
     const std::vector<Sequence>& sequences() const;
@@ -132,7 +135,7 @@ class Mussa
      */
     void load_motifs(std::istream &);
     //! load a list of motifs from a file named filename
-    void load_motifs(std::string filename);
+    void load_motifs(boost::filesystem::path filename);
     //! return our motifs;
     const std::set<std::string>& motifs() const;
 
index a71bda67e274ac53de6c3e12e01cf2728d7c4ed9..6c2acdf137da7ec87d3debb75d27627fa53a3e67 100644 (file)
 //                         ---------- mussa_nway.cc  -----------
 //                        ----------------------------------------
 
+#include <boost/filesystem/fstream.hpp>
+namespace fs = boost::filesystem;
+
 #include "alg/nway_paths.hpp"
 #include "alg/conserved_path.hpp"
 #include "mussa_exceptions.hpp"
 
-#include <fstream>
 #include <iostream>
 #include <stdexcept>
 
@@ -130,12 +132,12 @@ NwayPaths::add_path(ConservedPath loaded_path)
 
 
 void
-NwayPaths::save(string save_file_path)
+NwayPaths::save(fs::path save_file_path)
 {
-  fstream save_file;
+  fs::fstream save_file;
   list<ExtendedConservedPath >::iterator path_i, paths_end;
 
-  save_file.open(save_file_path.c_str(), ios::out);
+  save_file.open(save_file_path, ios::out);
 
   save_file << "<Mussa type=flp seq_count=" << sequence_count();
   save_file << " win=" << win_size;
@@ -180,18 +182,18 @@ NwayPaths::sequence_count()
 
 
 void
-NwayPaths::load(string load_file_path)
+NwayPaths::load(fs::path load_file_path)
 {
-  fstream load_file;
+  fs::fstream load_file;
   string file_data_line, header_data, data, path_node, path_width;
   int space_split_i, equal_split_i, comma_split_i, colon_split_i;
   vector<int> loaded_path;
 
-  load_file.open(load_file_path.c_str(), ios::in);
+  load_file.open(load_file_path, ios::in);
 
   if (!load_file)
   {
-    throw mussa_load_error("Sequence File: " + load_file_path + " not found");
+    throw mussa_load_error("Sequence File: " + load_file_path.string() + " not found");
   }
   else
   {
@@ -346,13 +348,13 @@ NwayPaths::find_paths_r(vector<vector<FLPs> > all_comparisons)
 
 
 void
-NwayPaths::save_old(string save_file_path)
+NwayPaths::save_old(fs::path save_file_path)
 {
-  fstream save_file;
+  fs::fstream save_file;
   list<ConservedPath >::iterator path_i, paths_end;
   size_t i;
 
-  save_file.open(save_file_path.c_str(), ios::app);
+  save_file.open(save_file_path, ios::app);
 
   path_i = pathz.begin();
   paths_end = pathz.end();
index 1a2b002abd741a5009ae28a5f7319c6bbbd61c4a..01c4be0f09a57ce3bd2d28a2bfb7facd71ba8c0d 100644 (file)
@@ -13,6 +13,7 @@
 //                        ----------------------------------------
 //                         ----------  mussa_nway.hh  -----------
 //                        ----------------------------------------
+#include <boost/filesystem/path.hpp>
 
 #include <list>
 #include <string>
@@ -55,9 +56,9 @@ class NwayPaths
     void path_search(std::vector<std::vector<FLPs> > all_comparisons, ConservedPath path, size_t depth);
 
     void simple_refine();
-    void save(std::string save_file_path);
+    void save(boost::filesystem::path save_file_path);
     //! load a muway file, \throws mussa_load_error
-    void load(std::string load_file_path);
+    void load(boost::filesystem::path load_file_path);
     void add_path(int threshold, std::vector<int>& loaded_path);
     void add_path(ConservedPath loaded_path);
     //! how many sequences are in our comparison
@@ -66,7 +67,7 @@ class NwayPaths
     void find_paths(std::vector<std::vector<FLPs> > all_comparisons);
     void refine();
 
-    void save_old(std::string save_file_path);
+    void save_old(boost::filesystem::path save_file_path);
     void print(std::list<std::vector<int> >&);
 
     std::list<ConservedPath>::iterator pbegin() { return pathz.begin() ; }
index 439fcb9ecf53ee17196547de4d559692f47493e9..7e7468b3f2ad3feaba2d3a1453ceb51039292ea7 100644 (file)
@@ -21,6 +21,7 @@
 //                        ----------------------------------------
 //                           ---------- sequence.cc -----------
 //                        ----------------------------------------
+#include <boost/filesystem/fstream.hpp>
 
 #include "alg/sequence.hpp"
 #include "mussa_exceptions.hpp"
@@ -28,6 +29,7 @@
 #include <string>
 #include <iostream>
 
+namespace fs = boost::filesystem;
 using namespace std;
 
 annot::annot() 
@@ -103,10 +105,10 @@ ostream& operator<<(ostream& out, const Sequence& seq)
  * \return error message, empty string if no error. (gag!)
  */
 void
-Sequence::load_fasta(string file_path, int seq_num, 
+Sequence::load_fasta(fs::path file_path, int seq_num, 
                      int start_index, int end_index)
 {
-  fstream data_file;
+  fs::fstream data_file;
   string file_data_line;
   int header_counter = 0;
   bool read_seq = true;
@@ -114,14 +116,14 @@ Sequence::load_fasta(string file_path, int seq_num,
   string sequence_raw;
   string seq_tmp;             // holds sequence during basic filtering
 
-  data_file.open(file_path.c_str(), ios::in);
+  data_file.open(file_path, ios::in);
 
   if (seq_num == 0) {
     throw mussa_load_error("fasta sequence number is 1 based (can't be 0)");
   }
   if (!data_file)
   {    
-    throw mussa_load_error("Sequence File: " + file_path + " not found"); 
+    throw mussa_load_error("Sequence File: " + file_path.string() + " not found"); 
   }
   // if file opened okay, read it
   else
@@ -203,9 +205,9 @@ void Sequence::set_filtered_sequence(const string &old_seq,
 
 
 void
-Sequence::load_annot(string file_path, int start_index, int end_index)
+Sequence::load_annot(fs::path file_path, int start_index, int end_index)
 {
-  fstream data_file;
+  fs::fstream data_file;
   string file_data_line;
   annot an_annot;
   string::size_type space_split_i;
@@ -215,11 +217,11 @@ Sequence::load_annot(string file_path, int start_index, int end_index)
 
 
   annots.clear();
-  data_file.open(file_path.c_str(), ios::in);
+  data_file.open(file_path, ios::in);
 
   if (!data_file)
   {
-    throw mussa_load_error("Sequence File: " + file_path + " not found");
+    throw mussa_load_error("Sequence File: " + file_path.string() + " not found");
   }
   // if file opened okay, read it
   else
@@ -285,8 +287,7 @@ Sequence::load_annot(string file_path, int start_index, int end_index)
           an_annot.end -= start_index;
           annots.push_back(an_annot);
         }
-        else
-          cout << "FAILED!!!!!!\n";
+        // else no (or bogus) annotations
       }
     }
 
@@ -448,7 +449,7 @@ Sequence::clear()
 }
 
 void
-Sequence::save(fstream &save_file)
+Sequence::save(fs::fstream &save_file)
                //string save_file_path)
 {
   //fstream save_file;
@@ -475,9 +476,9 @@ Sequence::save(fstream &save_file)
 }
 
 void
-Sequence::load_museq(string load_file_path, int seq_num)
+Sequence::load_museq(fs::path load_file_path, int seq_num)
 {
-  fstream load_file;
+  fs::fstream load_file;
   string file_data_line;
   int seq_counter;
   annot an_annot;
@@ -485,7 +486,7 @@ Sequence::load_museq(string load_file_path, int seq_num)
   string annot_value;
 
   annots.clear();
-  load_file.open(load_file_path.c_str(), ios::in);
+  load_file.open(load_file_path, ios::in);
 
   seq_counter = 0;
   // search for the seq_num-th sequence 
index 85fd692fa02475d9c369cd1dcb836500ad3d1966..06dd3fad5125560a37cd5d26c5c6bc08ad98118d 100644 (file)
@@ -14,8 +14,8 @@
 //                           ---------- sequence.hh -----------
 //                        ----------------------------------------
 
-
-#include <fstream>
+#include <boost/filesystem/path.hpp>
+#include <boost/filesystem/fstream.hpp>
 #include <list>
 #include <string>
 #include <vector>
@@ -83,12 +83,12 @@ class Sequence
     //! load sequence AGCT from fasta file
     /*! \throws mussa_load_error 
      */
-    void load_fasta(const std::string file_path, int seq_num=1, 
+    void load_fasta(const boost::filesystem::path file_path, int seq_num=1, 
                     int start_index=0, int end_index=0);
     //! load sequence annotations
     /*! \throws mussa_load_error 
      */
-    void load_annot(const std::string file_path, int start_index, int end_index);
+    void load_annot(const boost::filesystem::path file_path, int start_index, int end_index);
     const std::list<annot>& annotations() const;
     const std::list<motif>& motifs() const;
     const std::string& get_species() const;
@@ -127,7 +127,7 @@ class Sequence
     /*! \throws motif_normalize_error if there is an invalid symbol
      */
     static std::string motif_normalize(std::string a_motif);
-    void save(std::fstream &save_file);
-    void load_museq(std::string load_file_path, int seq_num); 
+    void save(boost::filesystem::fstream &save_file);
+    void load_museq(boost::filesystem::path load_file_path, int seq_num); 
 };
 #endif
index ecbd3aa6c1384465882c3f2e5a4afdc6b624681c..3e1e43b27e29e6c24909ff365b4babd720e03eaa 100644 (file)
@@ -2,11 +2,18 @@ SET(SOURCES test_annotation_color.cpp test_color.cpp test_conserved_path.cpp
             test_flp.cpp test_glseqbrowser.cpp test_glsequence.cpp
             test_main.cpp test_mussa.cpp test_nway.cpp test_sequence.cpp)
 
+# dont you love the number of \ you need to make sure the " shows up in
+# the C compiler?
+SET(EXAMPLE_DIR ${CMAKE_SOURCE_DIR}/examples)
+SET_SOURCE_FILES_PROPERTIES(${SOURCES} 
+                           COMPILE_FLAGS "-DEXAMPLE_DIR=\\\"${EXAMPLE_DIR}\\\""
+                           )
+
 FIND_PACKAGE(OpenGL)
 INCLUDE(FindBoost)
 
 ADD_EXECUTABLE(unittest ${SOURCES})
-ADD_TEST(core_test ${EXECUTABLE_OUTPUT_PATH}/alg/test/unittest)
+ADD_TEST(core_test ${CMAKE_BINARY_DIR}/alg/test/unittest)
 LINK_DIRECTORIES(${MUSSA_BINARY_DIR}/alg})
 TARGET_LINK_LIBRARIES(unittest 
                         ${BOOST_UNIT_TEST_LIB} 
index c3c9437a259fc8d68af8861ecf62ad3aae9f5a77..cc8500b16b8aabfa9cfb2eea6cf3f9b7dd5734eb 100644 (file)
@@ -4,12 +4,15 @@
 #include <boost/test/auto_unit_test.hpp>
 #include <boost/filesystem/operations.hpp>
 #include <boost/filesystem/path.hpp>
+namespace fs = boost::filesystem;
 
 #include "alg/flp.hpp"
 #include "alg/sequence.hpp"
 #include <iostream>
 #include <list>
 
+#include "mussa_exceptions.hpp"
+
 BOOST_AUTO_TEST_CASE( flp_simple )
 {
   FLPs f;
@@ -96,3 +99,10 @@ BOOST_AUTO_TEST_CASE( flp_reverse_compliment )
     // FIXME: should we test the individual lists?
   }
 }
+
+BOOST_AUTO_TEST_CASE( flp_file_check )
+{
+  fs::path filename = "Thy_micturations_are_to_me_as_plurdled_gabbleblotchits";
+  FLPs f1;
+  BOOST_CHECK_THROW( f1.load(filename), mussa_load_error);
+}
index 852d2fd0aa98bb80f67d95d3ebbf2d7de9255ba8..04886a0a1fd5675438d807576e481fc03ae60e90 100644 (file)
@@ -1,4 +1,6 @@
 #include <boost/test/auto_unit_test.hpp>
+#include <boost/filesystem/operations.hpp>
+namespace fs = boost::filesystem;
 
 #include <string>
 #include <sstream>
@@ -75,22 +77,21 @@ BOOST_AUTO_TEST_CASE ( empty_mussa_set_threshold )
   m.nway();
 }
 
-#include <unistd.h>
 BOOST_AUTO_TEST_CASE( mussa_load_mupa )
 {
+  fs::path mupa_path(EXAMPLE_DIR);
+  mupa_path /= "mck3test.mupa";
+
   Mussa m1;
-  chdir( "examples" );
-  m1.load_mupa_file( "mck3test.mupa" );
+  m1.load_mupa_file( mupa_path );
   m1.analyze(0, 0);
   BOOST_CHECK_EQUAL( m1.get_name(), std::string("mck3test") );
   BOOST_CHECK( m1.size() > 0 );
 
   Mussa m2;
-  std::string saved_analysis_path("mck3test_w30_t20");
-  m2.load( saved_analysis_path  );
-  chdir( ".." );
-
-  BOOST_CHECK_EQUAL( m2.get_name(), saved_analysis_path );
+  fs::path result_path = fs::initial_path() / "mck3test_w30_t20";
+  m2.load( result_path );
+  BOOST_CHECK_EQUAL( m2.get_name(), result_path.leaf() );
   BOOST_CHECK_EQUAL( m1.size(), m2.size() );
 
 }
@@ -98,14 +99,8 @@ BOOST_AUTO_TEST_CASE( mussa_load_mupa )
 BOOST_AUTO_TEST_CASE( mussa_load_full_path )
 {
   Mussa m1;
-  chdir( "examples" );
-  const int bufsize = 1024;
-  char path_buf[bufsize];
-  getcwd(path_buf, bufsize);
-  std::string path(path_buf);
-  chdir( ".." );
-  path += "/mck3test.mupa";
-  m1.load_mupa_file( path );
+  fs::path full_path(fs::path(EXAMPLE_DIR) / "mck3test.mupa");
+  m1.load_mupa_file( full_path );
   m1.analyze(0, 0);
 
   BOOST_CHECK( m1.size() > 0);
@@ -115,18 +110,13 @@ BOOST_AUTO_TEST_CASE( mussa_load_full_path )
 
 BOOST_AUTO_TEST_CASE( mussa_load_analysis )
 {
+  fs::path example_dir(EXAMPLE_DIR);
   Mussa m1;
-  chdir( "examples" );
-  const int bufsize = 1024;
-  char path_buf[bufsize];
-  getcwd(path_buf, bufsize);
-  std::string base_path(path_buf);
-  chdir( ".." );
-  m1.load_mupa_file( base_path + "/mck3test.mupa" );
+  m1.load_mupa_file( example_dir / "mck3test.mupa" );
   m1.analyze(0, 0);
 
   Mussa m2;
-  m2.load(base_path + "/mck3test_w30_t20");
+  m2.load( example_dir / "mck3test_w30_t20");
 
   BOOST_CHECK_EQUAL( m1.size(), m2.size() );
   BOOST_CHECK_EQUAL( m1.get_window(), m2.get_window() );
index 53508dfe1b121c3ea22d783fdb716d9ed2de0aa6..49be7e5c040a932e5c964932fa20cea66b3f6982 100644 (file)
@@ -1,4 +1,6 @@
 #include <boost/test/auto_unit_test.hpp>
+#include <boost/filesystem/path.hpp>
+namespace fs = boost::filesystem;
 
 #include <string>
 #include <iostream>
@@ -55,11 +57,12 @@ BOOST_AUTO_TEST_CASE( nway_test )
   }
 }
 
-#include <unistd.h>
 BOOST_AUTO_TEST_CASE( nway_refine )
 {
+  fs::path mupa_path( EXAMPLE_DIR );
+  mupa_path /= "mck3test.mupa";
   Mussa m1;
-  m1.load_mupa_file( "examples/mck3test.mupa" );
+  m1.load_mupa_file( mupa_path );
   m1.analyze(0, 0);
   const NwayPaths& npath = m1.paths();
   BOOST_CHECK_EQUAL (npath.path_size(), npath.refined_path_size());
index bbc5068d951390895bc9e79cee8373b3ffe819f5..1a4e9ac738598af56be6614975170676656a1714 100644 (file)
@@ -1,4 +1,8 @@
 #include <boost/test/auto_unit_test.hpp>
+#include <boost/filesystem/path.hpp>
+#include <boost/filesystem/operations.hpp>
+namespace fs=boost::filesystem;
+
 #include <list>
 #include <iostream>
 
@@ -49,8 +53,10 @@ BOOST_AUTO_TEST_CASE( sequence_filter )
 //! Can we load data from a file
 BOOST_AUTO_TEST_CASE( sequence_load )
 {
+  fs::path seq_path(fs::path(EXAMPLE_DIR)/ "seq" );
+  seq_path /=  "human_mck_pro.fa";
   Sequence s;
-  s.load_fasta("examples/seq/human_mck_pro.fa");
+  s.load_fasta(seq_path);
   BOOST_CHECK_EQUAL(s.subseq(0, 5), "GGATC"); // first few chars of fasta file
   BOOST_CHECK_EQUAL(s.get_header(), "gi|180579|gb|M21487.1|HUMCKMM1 Human "
                                     "muscle creatine kinase gene (CKMM), "
index 56d7990934a1a15344b273f10aea2af44f7b869a..c085ca4cd0830abb8f9b7221a999e2333340ac4a 100644 (file)
@@ -62,5 +62,6 @@ TARGET_LINK_LIBRARIES(mussagl
                         ${QT_QTOPENGL_LIBRARY}
                         ${OPENGL_gl_LIBRARY}
                         ${BOOST_PROGRAM_OPTIONS_LIB}
+                        ${BOOST_FILESYSTEM_LIB}
                         )
 
index 656c9a2de45f9340efad2f6d9db0942a9d0f32b2..28f7bc592976441cd7ef21f0be3101396c578761 100644 (file)
@@ -1,3 +1,6 @@
+#include <boost/filesystem/operations.hpp>
+using namespace boost::filesystem;
+
 #include <QApplication>
 
 #include "qui/MussaWindow.hpp"
@@ -5,6 +8,9 @@
 
 int main(int argc, char **argv)
 {
+  // let boost::filesystem store the path we were initially launched in
+  initial_path();
+
   QApplication app(argc, argv);
   Q_INIT_RESOURCE(icons);
   Mussa *analysis = initialize_mussa(argc, argv);