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