switch to using boost::filesystem
[mussa.git] / alg / test / test_flp.cpp
1 #include <iostream>
2 #include <list>
3
4 #include <boost/test/auto_unit_test.hpp>
5 #include <boost/filesystem/operations.hpp>
6 #include <boost/filesystem/path.hpp>
7 namespace fs = boost::filesystem;
8
9 #include "alg/flp.hpp"
10 #include "alg/sequence.hpp"
11 #include <iostream>
12 #include <list>
13
14 #include "mussa_exceptions.hpp"
15
16 BOOST_AUTO_TEST_CASE( flp_simple )
17 {
18   FLPs f;
19   f.setup(5, 4);
20   f.seqcomp("AAGGTAAGGT", "AAGGTAAGGT", false);
21   
22   // are the match_locations correct?
23   for (int i=0; i != f.size(); i++)
24   {
25     std::list<int> window_locations = f.match_locations(i);
26     std::list<FLPs::match> window_matches   = f.matches(i);
27     std::list<int>::iterator loc_i   = window_locations.begin();
28     std::list<FLPs::match>::iterator match_i = window_matches.begin();
29     for (; loc_i != window_locations.end(); ++loc_i, ++match_i)
30     {
31       // I'm missing pythons easy lists and in operator here.
32       if (i == 0) BOOST_CHECK( *loc_i == 0 || *loc_i == 5);
33       else if (i == 1) BOOST_CHECK( *loc_i == 1 || *loc_i == 6);
34       else if (i == 2) BOOST_CHECK( *loc_i == 2 );
35       else if (i == 3) BOOST_CHECK( *loc_i == 3 );
36       else if (i == 4) BOOST_CHECK( *loc_i == 4 );
37       else if (i == 6) BOOST_CHECK( *loc_i == 5 || *loc_i == 0);
38       BOOST_CHECK_EQUAL( *loc_i, match_i->index );
39     }
40   }
41 }
42
43 BOOST_AUTO_TEST_CASE( flp_save )
44 {
45   std::string seq("AAGGCCTTAAGGCCTT");
46   int win_size = 4;
47   FLPs f1;
48   FLPs f2;
49   f1.setup(win_size,3);
50   f1.seqcomp(seq, seq, false);
51   std::string fname("flp_save_this_is_a_horrible_filename_because_"
52                     "im_too_lazy_to_write_a_portable_mktmp.flp");
53                     
54   f1.save(fname);
55   f2.load(fname);
56   boost::filesystem::remove( boost::filesystem::path(fname) );
57
58   BOOST_CHECK_EQUAL( f1.size(), seq.size()-win_size+1);
59   BOOST_CHECK_EQUAL( f2.size(), seq.size()-win_size+1);
60   BOOST_CHECK_EQUAL( f1.size(), f2.size() );
61   for (int win=0; win < f1.size(); ++win)
62   {
63     std::list<FLPs::match> f1_matches = f1.matches(win);
64     std::list<FLPs::match> f2_matches = f2.matches(win);
65     std::list<FLPs::match>::const_iterator f1_match_i = f1_matches.begin();
66     std::list<FLPs::match>::const_iterator f2_match_i = f2_matches.begin();
67     for( ; 
68          f1_match_i != f1_matches.end() && f2_match_i != f2_matches.end();
69          ++f1_match_i, ++f2_match_i)
70     {
71       BOOST_CHECK_EQUAL( *f1_match_i, *f2_match_i);
72     }
73   }
74 }
75
76 /*! Apparently when we run multiple seqcomps we want to 
77  *  save all the FLPs generated
78  */
79 BOOST_AUTO_TEST_CASE( flp_reverse_compliment )
80 {
81   FLPs f1;
82   Sequence s1("AAAATTTT");
83   Sequence s2("AACAGGGG");
84   f1.setup(4,3);
85   f1.seqcomp(s1.get_seq(), s2.get_seq(), false);
86   f1.seqcomp(s1.get_seq(), s2.rev_comp(), true);
87
88   FLPs f2;
89   f2.setup(4,3);
90   f2.seqcomp(s1.get_seq(), s2.rev_comp(), true);
91   f2.seqcomp(s1.get_seq(), s2.get_seq(), false);
92
93   // The order of doing the reverse compliment search shouldn't matter
94   // when we're using exactly the same sequence
95   BOOST_CHECK_EQUAL( f1.size(), f2.size() );
96   for (int i = 0; i < f1.size(); ++i )
97   {
98     BOOST_CHECK_EQUAL( f1.matches(i).size(), f2.matches(i).size() );
99     // FIXME: should we test the individual lists?
100   }
101 }
102
103 BOOST_AUTO_TEST_CASE( flp_file_check )
104 {
105   fs::path filename = "Thy_micturations_are_to_me_as_plurdled_gabbleblotchits";
106   FLPs f1;
107   BOOST_CHECK_THROW( f1.load(filename), mussa_load_error);
108 }