Getting closer to a subanalysis mode
[mussa.git] / alg / test / test_mussa.cpp
1 #include <boost/test/auto_unit_test.hpp>
2 #include <boost/filesystem/operations.hpp>
3 namespace fs = boost::filesystem;
4 #include <boost/assign/list_of.hpp>
5 #include <boost/assign/list_inserter.hpp>
6 #include <boost/assign.hpp>
7 namespace assign = boost::assign;
8
9 #include <string>
10 #include <sstream>
11 #include <vector>
12
13 #include "alg/mussa.hpp"
14
15 using namespace std;
16
17 //! can we initialize a mussa object?
18 BOOST_AUTO_TEST_CASE( mussa_simple )
19 {
20   Mussa m;
21   BOOST_CHECK_EQUAL(m.get_name(), "" );
22   BOOST_CHECK_EQUAL(m.get_window(), 0);
23   BOOST_CHECK_EQUAL(m.get_threshold(), 0);
24   BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::TransitiveNway);
25   m.set_name( "hello" );
26   BOOST_CHECK_EQUAL(m.get_name(), "hello" );
27   m.set_window(30);
28   BOOST_CHECK_EQUAL(m.get_window(), 30);
29   m.set_threshold(21);
30   BOOST_CHECK_EQUAL(m.get_threshold(), 21);
31   BOOST_CHECK_EQUAL(m.get_soft_threshold(), 21);
32   m.set_soft_threshold(19);
33   BOOST_CHECK_EQUAL(m.get_soft_threshold(), 21);
34   m.set_soft_threshold(35);
35   BOOST_CHECK_EQUAL(m.get_soft_threshold(), 30);
36   m.set_soft_threshold(25);
37   BOOST_CHECK_EQUAL(m.get_soft_threshold(), 25);
38   m.set_analysis_mode(Mussa::RadialNway);
39   BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::RadialNway);
40     
41   m.clear();
42   BOOST_CHECK_EQUAL(m.get_name(), "" );
43   BOOST_CHECK_EQUAL(m.get_window(), 0);
44   BOOST_CHECK_EQUAL(m.get_threshold(), 0);
45   BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::TransitiveNway);
46 }
47
48 BOOST_AUTO_TEST_CASE( mussa_analysis_name )
49 {
50   Mussa m;
51   m.set_analysis_mode( Mussa::TransitiveNway );
52   BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Transitive" );
53   m.set_analysis_mode( Mussa::RadialNway );
54   BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Radial" );
55   m.set_analysis_mode( Mussa::EntropyNway );
56   BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Entropy" );
57   m.set_analysis_mode( Mussa::RecursiveNway);
58   BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "[deprecated] Recursive" );
59 }
60
61 BOOST_AUTO_TEST_CASE( mussa_sequences )
62 {
63   std::string s0("AAAANNNN");
64   std::string s1("GGGGNNNN");
65   std::string s2("TTTTNNNN");
66
67   Mussa analysis;
68   analysis.append_sequence(s0);
69   analysis.append_sequence(s1);
70   analysis.append_sequence(s2);
71
72   BOOST_CHECK_EQUAL( analysis.sequences().size(), 3 );
73   BOOST_CHECK_EQUAL( *(analysis.sequences()[0]), s0);
74   BOOST_CHECK_EQUAL( *(analysis.sequences()[1]), s1);
75   BOOST_CHECK_EQUAL( *(analysis.sequences()[2]), s2);
76 }
77
78 // for some reason we can call nway once safely but it
79 // somehow changed things and caused a segfault
80 // fixed by adding a return statement in trans_path_search 
81 BOOST_AUTO_TEST_CASE ( empty_mussa_set_threshold )
82 {
83   Mussa m;
84   m.set_soft_threshold(15);
85   m.nway();
86
87   m.set_soft_threshold(25);
88   m.nway();
89 }
90
91 BOOST_AUTO_TEST_CASE( mussa_load_mupa )
92 {
93   fs::path mupa_path(EXAMPLE_DIR);
94   mupa_path /= "mck3test.mupa";
95
96   Mussa m1;
97   m1.load_mupa_file( mupa_path );
98   m1.analyze();
99   BOOST_CHECK_EQUAL( m1.get_name(), std::string("mck3test") );
100   BOOST_CHECK( m1.size() > 0 );
101
102   Mussa m2;
103   fs::path result_path = fs::initial_path() / "mck3test_w30_t20";
104   m2.load( result_path );
105   BOOST_CHECK_EQUAL( m2.get_name(), result_path.leaf() );
106   BOOST_CHECK_EQUAL( m1.size(), m2.size() );
107
108 }
109
110 BOOST_AUTO_TEST_CASE( mussa_load_full_path )
111 {
112   Mussa m1;
113   fs::path full_path(fs::path(EXAMPLE_DIR) / "mck3test.mupa");
114   m1.load_mupa_file( full_path );
115   m1.analyze();
116
117   BOOST_CHECK( m1.size() > 0);
118   BOOST_CHECK_EQUAL( m1.get_window(), 30 );
119   BOOST_CHECK_EQUAL( m1.get_threshold(), 20);
120 }
121
122 BOOST_AUTO_TEST_CASE( mussa_load_analysis )
123 {
124   fs::path example_dir(EXAMPLE_DIR);
125   Mussa m1;
126   m1.load_mupa_file( example_dir / "mck3test.mupa" );
127   m1.analyze();
128
129   Mussa m2;
130   m2.load( fs::initial_path() / "mck3test_w30_t20");
131
132   BOOST_CHECK_EQUAL( m1.size(), m2.size() );
133   BOOST_CHECK_EQUAL( m1.get_window(), m2.get_window() );
134   BOOST_CHECK_EQUAL( m1.get_threshold(), m2.get_threshold() );
135 }
136
137 BOOST_AUTO_TEST_CASE( mussa_load_motif )
138 {
139   string data = "AAGG 1.0 1.0 0.0\n"
140                 "GGTT 0.0 0.1 1.0\n"
141                 "ZXY 2 1.9 0\n";
142
143   istringstream test_istream(data);
144
145   Mussa m1;
146   m1.append_sequence("AAAAGGGGTTTT");
147   m1.append_sequence("GGGCCCCTTGGTT");
148   m1.load_motifs(test_istream);
149
150   for (Mussa::vector_sequence_type::const_iterator seq_i = m1.sequences().begin();
151        seq_i != m1.sequences().end();
152        ++seq_i)
153   {
154     BOOST_CHECK( (*seq_i)->motifs().size() > 0 );
155   }
156 }
157
158 BOOST_AUTO_TEST_CASE( mussa_add_motif )
159 {
160   vector<string> motifs;
161   motifs.push_back("AAGG");
162   vector<Color> colors;
163   colors.push_back(Color(1.0, 0.0, 0.0));
164   
165   Mussa m1;
166   m1.append_sequence("AAAAGGGGTTTT");
167   m1.append_sequence("GGGCCCCTTGGTT");
168   m1.add_motifs(motifs, colors);
169   int first_size = m1.motifs().size();
170   BOOST_CHECK_EQUAL( first_size, 1 );
171   m1.add_motifs(motifs, colors);
172   BOOST_CHECK_EQUAL( first_size, m1.motifs().size() );
173 }
174
175 static void 
176 local_align_test(const Mussa::vector_sequence_type &seqs, 
177                  const list<ConservedPath::path_type>& result,
178                  const list<vector<bool> >& reversed)
179 {
180   map<char, vector <char> >  m;
181   assign::insert(m)('A', assign::list_of('A')('T') )
182                    ('T', assign::list_of('T')('A') )
183                    ('G', assign::list_of('G')('C') )
184                    ('C', assign::list_of('C')('G') );
185   list<vector<bool> >::const_iterator rc_i = reversed.begin();
186
187   for(list<ConservedPath::path_type>::const_iterator base_i = result.begin();
188       base_i != result.end();
189       ++base_i, ++rc_i)
190   {
191     // since the reverse compliment flag is relative to the first sequence
192     // the first one should always be false
193     BOOST_CHECK_EQUAL( (*rc_i)[0], false );
194     const int first_path_basepair_index = (*base_i)[0];
195     const int second_path_basepair_index = (*base_i)[1];
196     const char first_basepair = (*seqs[0])[first_path_basepair_index];
197     const char second_basepair = (*seqs[1])[second_path_basepair_index];
198     // get our index into our reverse compliment map m
199     const int second_compliment_index = (*rc_i)[1];
200     // lookup the forward or reverse compliment depending on our rc flag
201     const char complimented_second = m[second_basepair][second_compliment_index];
202    
203     BOOST_CHECK_EQUAL( first_basepair, complimented_second) ;
204   }
205 }
206
207                  
208 BOOST_AUTO_TEST_CASE( local_alignment )
209 {
210   string s0("GCGCATAT");
211   string s1("AAAAAAAT");
212   Sequence seq1(s1);
213
214   Mussa analysis;
215   analysis.append_sequence(s0);
216   analysis.append_sequence(s1);
217   analysis.set_threshold(3);
218   analysis.set_window(4);
219   analysis.analyze();
220   NwayPaths npath = analysis.paths();
221   list<ConservedPath::path_type> result;
222   list<vector<bool> > reversed;
223   list<ConservedPath>::iterator pathz_i = npath.pathz.begin();
224
225   list<ConservedPath> selected_paths;
226   selected_paths.push_back(*pathz_i);
227   analysis.createLocalAlignment(selected_paths.begin(), 
228                                 selected_paths.end(),
229                                 result,
230                                 reversed);
231
232   local_align_test(analysis.sequences(), result, reversed);
233
234   ++pathz_i;
235   result.clear();
236   reversed.clear();
237   selected_paths.clear();
238   selected_paths.push_back(*pathz_i);
239   analysis.createLocalAlignment(selected_paths.begin(), 
240                                 selected_paths.end(),
241                                 result,
242                                 reversed);
243   local_align_test(analysis.sequences(), result, reversed);
244
245
246 }
247
248 BOOST_AUTO_TEST_CASE( subanalysis )
249 {
250   Sequence s1("AATGAAGATTTTAATGCTTTAATTTTGTTTTGTAAACTTCGAATTTCCAAAATTTGAAA");
251   Sequence s2("AGGAGCAAGTTCGCTTCATCGAGAATTTTTAATTTTTAGTCAAATTTTCCAATGTCTGA");
252
253   Mussa analysis;
254   analysis.append_sequence(s1);
255   analysis.append_sequence(s2);
256   analysis.set_threshold(8);
257   analysis.set_window(8);
258   analysis.analyze();
259
260   NwayPaths perfect_path = analysis.paths();
261   int perfect_match_count = perfect_path.pathz.size();
262
263   Sequence sub1 = s1.subseq(2, s1.size()-4);
264   Sequence sub2 = s2.subseq(2, s2.size()-4);
265   Mussa subanalysis;
266   subanalysis.append_sequence(sub1);
267   subanalysis.append_sequence(sub2);
268   subanalysis.set_threshold(7);
269   subanalysis.set_window(8);
270   subanalysis.analyze();
271   NwayPaths one_mismatch_path = subanalysis.paths();
272   int one_mismatch_count = one_mismatch_path.pathz.size();
273
274   BOOST_CHECK( perfect_match_count < one_mismatch_count );
275 }
276