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