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