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