1 #define BOOST_AUTO_TEST_MAIN
2 #include <boost/test/auto_unit_test.hpp>
3 #include <boost/filesystem/path.hpp>
4 #include <boost/filesystem/operations.hpp>
5 namespace fs = boost::filesystem;
6 #include <boost/assign/list_of.hpp>
7 #include <boost/assign/list_inserter.hpp>
8 #include <boost/assign.hpp>
9 namespace assign = boost::assign;
15 #include "alg/mussa.hpp"
16 #include "mussa_exceptions.hpp"
20 //! can we initialize a mussa object?
21 BOOST_AUTO_TEST_CASE( mussa_simple )
24 BOOST_CHECK_EQUAL(m.empty(), true);
25 BOOST_CHECK_EQUAL(m.get_name(), "" );
26 BOOST_CHECK_EQUAL(m.get_window(), 0);
27 BOOST_CHECK_EQUAL(m.get_threshold(), 0);
28 BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::TransitiveNway);
29 m.set_name( "hello" );
30 BOOST_CHECK_EQUAL(m.get_name(), "hello" );
32 BOOST_CHECK_EQUAL(m.get_window(), 30);
34 BOOST_CHECK_EQUAL(m.get_threshold(), 21);
35 BOOST_CHECK_EQUAL(m.get_soft_threshold(), 21);
36 m.set_soft_threshold(19);
37 BOOST_CHECK_EQUAL(m.get_soft_threshold(), 21);
38 m.set_soft_threshold(35);
39 BOOST_CHECK_EQUAL(m.get_soft_threshold(), 30);
40 m.set_soft_threshold(25);
41 BOOST_CHECK_EQUAL(m.get_soft_threshold(), 25);
42 m.set_analysis_mode(Mussa::RadialNway);
43 BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::RadialNway);
44 // make sure our path is empty
45 BOOST_CHECK_EQUAL(m.get_analysis_path().string(), fs::path().string() );
48 BOOST_CHECK_EQUAL(m.get_name(), "" );
49 BOOST_CHECK_EQUAL(m.get_window(), 0);
50 BOOST_CHECK_EQUAL(m.get_threshold(), 0);
51 BOOST_CHECK_EQUAL(m.get_analysis_mode(), Mussa::TransitiveNway);
54 BOOST_AUTO_TEST_CASE ( mussa_title )
58 BOOST_CHECK_EQUAL( m.get_title(), "Unnamed");
61 BOOST_CHECK_EQUAL( m.get_title(), foo);
62 string foopath_name("/my/silly/path");
63 fs::path foopath(foopath_name);
64 m.set_analysis_path(foopath);
65 BOOST_CHECK_EQUAL( m.get_title().size(), 14);
68 BOOST_AUTO_TEST_CASE( mussa_analysis_name )
71 m.set_analysis_mode( Mussa::TransitiveNway );
72 BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Transitive" );
73 m.set_analysis_mode( Mussa::RadialNway );
74 BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Radial" );
75 m.set_analysis_mode( Mussa::EntropyNway );
76 BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "Entropy" );
77 m.set_analysis_mode( Mussa::RecursiveNway);
78 BOOST_CHECK_EQUAL( m.get_analysis_mode_name(), "[deprecated] Recursive" );
81 BOOST_AUTO_TEST_CASE( mussa_sequences )
83 std::string s0("AAAANNNN");
84 std::string s1("GGGGNNNN");
85 std::string s2("TTTTNNNN");
88 BOOST_CHECK_EQUAL(analysis.empty(), true);
89 analysis.append_sequence(s0);
90 analysis.append_sequence(s1);
91 analysis.append_sequence(s2);
93 BOOST_CHECK_EQUAL( analysis.empty(), false);
94 BOOST_CHECK_EQUAL( analysis.sequences().size(), 3 );
95 BOOST_CHECK_EQUAL( *(analysis.sequences()[0]), s0);
96 BOOST_CHECK_EQUAL( *(analysis.sequences()[1]), s1);
97 BOOST_CHECK_EQUAL( *(analysis.sequences()[2]), s2);
100 // for some reason we can call nway once safely but it
101 // somehow changed things and caused a segfault
102 // fixed by adding a return statement in trans_path_search
103 BOOST_AUTO_TEST_CASE ( empty_mussa_set_threshold )
106 m.set_soft_threshold(15);
109 m.set_soft_threshold(25);
113 BOOST_AUTO_TEST_CASE( mussa_load_mupa )
115 fs::path mupa_path(EXAMPLE_DIR, fs::native);
116 fs::path result_path = fs::initial_path() / "mck3test_w30_t20";
117 mupa_path /= "mck3test.mupa";
120 m1.load_mupa_file( mupa_path );
122 m1.save( result_path );
123 BOOST_CHECK_EQUAL( m1.empty(), false);
124 BOOST_CHECK_EQUAL( m1.get_name(), std::string("mck3test") );
125 BOOST_CHECK( m1.size() > 0 );
126 BOOST_CHECK_EQUAL( m1.get_analysis_path().string(), result_path.string());
129 m2.load( result_path );
130 BOOST_CHECK_EQUAL( m2.empty(), false);
131 BOOST_CHECK_EQUAL( m2.get_name(), result_path.leaf() );
132 BOOST_CHECK_EQUAL( m1.size(), m2.size() );
133 BOOST_CHECK_EQUAL( result_path.string(), m2.get_analysis_path().string() );
137 BOOST_CHECK_EQUAL( m2.empty(), true);
138 BOOST_CHECK_EQUAL( m2.is_dirty(), false );
139 BOOST_CHECK_EQUAL( m2.get_analysis_path().string(), fs::path().string());
142 BOOST_AUTO_TEST_CASE( mussa_load_full_path )
145 fs::path full_path(fs::path(EXAMPLE_DIR, fs::native) / "mck3test.mupa");
146 m1.load_mupa_file( full_path );
149 BOOST_CHECK( m1.size() > 0);
150 BOOST_CHECK_EQUAL( m1.get_window(), 30 );
151 BOOST_CHECK_EQUAL( m1.get_threshold(), 20);
152 BOOST_CHECK_EQUAL( m1.is_dirty(), true);
153 BOOST_CHECK_EQUAL( m1.get_analysis_path().string(), "");
156 // make sure we know that mupa files cannot be directories
157 BOOST_AUTO_TEST_CASE( mussa_mupa_is_file_not_directory )
159 fs::path curdir(".");
161 BOOST_CHECK_THROW(m1.load_mupa_file( curdir ), mussa_load_error );
164 // catch error if annotation isn't a file
165 BOOST_AUTO_TEST_CASE( mussa_annotation_is_not_file )
168 fs::path full_path(fs::path(EXAMPLE_DIR, fs::native) / "directory.mupa");
169 BOOST_CHECK_THROW( m1.load_mupa_file( full_path ), mussa_load_error );
172 BOOST_AUTO_TEST_CASE( mussa_load_analysis )
174 fs::path example_dir(EXAMPLE_DIR, fs::native);
176 m1.load_mupa_file( example_dir / "mck3test.mupa" );
180 fs::path analysis_path = fs::initial_path() / "mck3test_w30_t20";
181 m2.load( analysis_path );
183 BOOST_CHECK_EQUAL( m1.size(), m2.size() );
184 BOOST_CHECK_EQUAL( m1.get_window(), m2.get_window() );
185 BOOST_CHECK_EQUAL( m1.get_threshold(), m2.get_threshold() );
186 BOOST_CHECK_EQUAL( m2.get_analysis_path().string(), analysis_path.string());
189 BOOST_AUTO_TEST_CASE( mussa_load_motif )
191 string data = "AAGG 1.0 1.0 0.0\n"
192 "GGTT 0.0 0.1 1.0 1.0\n";
194 istringstream test_istream(data);
197 m1.append_sequence("AAAAGGGGTTTT");
198 m1.append_sequence("GGGCCCCTTCCAATT");
199 m1.load_motifs(test_istream);
201 BOOST_CHECK_EQUAL( m1.motifs().size(), 2);
202 for (Mussa::vector_sequence_type::const_iterator seq_i = m1.sequences().begin();
203 seq_i != m1.sequences().end();
206 BOOST_CHECK( (*seq_i)->motifs().size() > 0 );
210 BOOST_AUTO_TEST_CASE( mussa_load_broken_motif )
212 string data = "AAGG 1.0 1.0 0.0\n"
213 "GGTT 0.0 0.1 1.0 1.0\n"
214 "ZZCTA 0.1 0.0 1.0\n";
216 istringstream test_istream(data);
219 m1.append_sequence("AAAAGGGGTTTT");
220 m1.append_sequence("GGGCCCCTTCCAATT");
221 BOOST_CHECK_THROW(m1.load_motifs(test_istream), motif_load_error);
223 BOOST_CHECK_EQUAL( m1.motifs().size(), 0);
226 BOOST_AUTO_TEST_CASE( mussa_named_motif )
228 string data = "CCAATT cat 0.1 0.2 0.3\n";
229 istringstream test_istream(data);
232 m1.append_sequence("AAAAGGGGTTTT");
233 m1.append_sequence("GGGCCCCTTCCAATT");
234 m1.load_motifs(test_istream);
236 std::set<Sequence> motifs = m1.motifs();
237 BOOST_REQUIRE_EQUAL(motifs.size(), 1);
238 BOOST_CHECK_EQUAL(motifs.begin()->get_name(), "cat");
241 BOOST_AUTO_TEST_CASE( mussa_weirdly_spaced_named_motif )
243 string data = "CCAATT cat_meow123 0.1 0.2 0.3\n";
244 istringstream test_istream(data);
247 m1.append_sequence("AAAAGGGGTTTT");
248 m1.append_sequence("GGGCCCCTTCCAATT");
249 m1.load_motifs(test_istream);
251 std::set<Sequence> motifs = m1.motifs();
252 BOOST_REQUIRE_EQUAL(motifs.size(), 1);
253 BOOST_CHECK_EQUAL(motifs.begin()->get_name(), "cat_meow123");
256 BOOST_AUTO_TEST_CASE( mussa_name_quoted_motif )
258 string data = "CCAATT \"cat meow 123\" 0.1 0.2 0.3\n";
259 istringstream test_istream(data);
262 m1.append_sequence("AAAAGGGGTTTT");
263 m1.append_sequence("GGGCCCCTTCCAATT");
264 m1.load_motifs(test_istream);
266 std::set<Sequence> motifs = m1.motifs();
267 BOOST_REQUIRE_EQUAL(motifs.size(), 1);
268 BOOST_CHECK_EQUAL(motifs.begin()->get_name(), "cat meow 123");
271 BOOST_AUTO_TEST_CASE( mussa_name_embedded_quote_motif )
273 // pretty obviously this shouldn't work as " are our delimiter
274 // and i'm too lazy to add support for \ in the parser
275 string data = "ATA 0.5 0.5 0.5\n"
276 "CCAATT \"cat \"meow 123\" 0.1 0.2 0.3\n";
277 istringstream test_istream(data);
280 m1.append_sequence("AAAAGGGGTTTT");
281 m1.append_sequence("GGGCCCCTTCCAATT");
282 BOOST_CHECK_THROW( m1.load_motifs(test_istream), motif_load_error);
284 std::set<Sequence> motifs = m1.motifs();
285 BOOST_REQUIRE_EQUAL(motifs.size(), 0);
288 BOOST_AUTO_TEST_CASE( mussa_save_motif )
290 string data = "ATA 1 1 1 1\n"
291 "CAT \"my name\" 1 0 0.5 0.5\n";
292 istringstream data_istream(data);
295 m1.append_sequence("AAAAGGGGTTTT");
296 m1.append_sequence("GGGCCCCTTCCAATT");
297 m1.load_motifs(data_istream);
300 ostringstream save_ostream(save);
301 m1.save_motifs(save_ostream);
303 istringstream reloaded_istream(save_ostream.str());
305 m2.append_sequence("AAAAGGGGTTTT");
306 m2.append_sequence("GGGCCCCTTCCAATT");
307 m2.load_motifs(reloaded_istream);
309 BOOST_REQUIRE_EQUAL(m1.motifs().size(), m2.motifs().size());
310 Mussa::motif_set::const_iterator m1motif = m1.motifs().begin();
311 Mussa::motif_set::const_iterator m2motif = m2.motifs().begin();
313 m1motif != m1.motifs().end() and m2motif != m2.motifs().end();
314 ++m1motif, ++m2motif)
316 BOOST_CHECK_EQUAL(m1motif->get_sequence(), m2motif->get_sequence());
317 BOOST_CHECK_EQUAL(m1motif->get_name(), m2motif->get_name());
318 BOOST_CHECK_EQUAL(m1.colorMapper()->lookup("motif", m1motif->get_sequence()),
319 m2.colorMapper()->lookup("motif", m2motif->get_sequence()));
323 BOOST_AUTO_TEST_CASE( mussa_add_motif )
325 vector<Sequence> motifs;
326 motifs.push_back("AAGG");
327 vector<Color> colors;
328 colors.push_back(Color(1.0, 0.0, 0.0));
331 m1.append_sequence("AAAAGGGGTTTT");
332 m1.append_sequence("GGGCCCCTTGGTT");
333 m1.set_motifs(motifs, colors);
334 int first_size = m1.motifs().size();
335 BOOST_CHECK_EQUAL( first_size, 1 );
336 BOOST_REQUIRE(first_size > 0);
337 BOOST_CHECK_EQUAL(*(m1.motifs().begin()), motifs.front());
338 // make sure that our sequences have the right number of motifs
339 BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 1);
340 BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1); // because of rc
342 // verify that setting the motif clears the arrays
343 m1.set_motifs(motifs, colors);
344 BOOST_CHECK_EQUAL( first_size, m1.motifs().size() );
345 // make sure that our sequences have the right number of motifs
346 BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 1);
347 BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1);
349 // add a different motif
351 motifs.push_back("CCTTGG");
352 BOOST_CHECK_EQUAL(motifs.size(), 1);
353 m1.set_motifs(motifs, colors);
354 BOOST_CHECK_EQUAL(m1.motifs().size(), 1);
355 BOOST_REQUIRE(m1.motifs().size() > 0);
356 BOOST_CHECK_EQUAL(*(m1.motifs().begin()), motifs.front());
357 BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 0);
358 BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1);
360 // try a motif that doesn't exist
362 motifs.push_back("CCTTGG");
363 BOOST_CHECK_EQUAL(motifs.size(), 1);
364 m1.set_motifs(motifs, colors);
365 BOOST_CHECK_EQUAL(m1.motifs().size(), 1);
366 BOOST_CHECK_EQUAL(m1.sequences()[0]->motifs().size(), 0);
367 BOOST_CHECK_EQUAL(m1.sequences()[1]->motifs().size(), 1);
372 two_way_local_align_test(const Mussa::vector_sequence_type &seqs,
373 const list<ConservedPath::path_type>& result,
374 const list<vector<bool> >& reversed)
376 map<char, vector <char> > m;
377 assign::insert(m)('A', assign::list_of('A')('T') )
378 ('T', assign::list_of('T')('A') )
379 ('G', assign::list_of('G')('C') )
380 ('C', assign::list_of('C')('G') );
381 list<vector<bool> >::const_iterator rc_i = reversed.begin();
383 for(list<ConservedPath::path_type>::const_iterator base_i = result.begin();
384 base_i != result.end();
387 // since the reverse compliment flag is relative to the first sequence
388 // the first one should always be false
389 BOOST_CHECK_EQUAL( (*rc_i)[0], false );
390 const int first_path_basepair_index = (*base_i)[0];
391 const int second_path_basepair_index = (*base_i)[1];
392 const char first_basepair = (*seqs[0])[first_path_basepair_index];
393 const char second_basepair = (*seqs[1])[second_path_basepair_index];
394 // get our index into our reverse compliment map m
395 const int second_compliment_index = (*rc_i)[1];
396 // lookup the forward or reverse compliment depending on our rc flag
397 const char complimented_second = m[second_basepair][second_compliment_index];
399 BOOST_CHECK_EQUAL( first_basepair, complimented_second) ;
403 BOOST_AUTO_TEST_CASE( two_way_local_alignment )
405 string s0("GCGCATAT");
406 string s1("AAAAAAAT");
410 analysis.append_sequence(s0);
411 analysis.append_sequence(s1);
412 analysis.set_threshold(3);
413 analysis.set_window(4);
415 NwayPaths npath = analysis.paths();
416 BOOST_REQUIRE_EQUAL( npath.pathz.size(), 2 );
418 list<ConservedPath::path_type> result;
419 list<vector<bool> > reversed;
420 list<ConservedPath>::iterator pathz_i = npath.pathz.begin();
422 list<ConservedPath> selected_paths;
423 selected_paths.push_back(*pathz_i);
424 analysis.createLocalAlignment(selected_paths.begin(),
425 selected_paths.end(),
429 two_way_local_align_test(analysis.sequences(), result, reversed);
434 selected_paths.clear();
435 selected_paths.push_back(*pathz_i);
436 analysis.createLocalAlignment(selected_paths.begin(),
437 selected_paths.end(),
440 two_way_local_align_test(analysis.sequences(), result, reversed);
443 BOOST_AUTO_TEST_CASE( three_way_local_alignment )
445 string s0("AGCAGGGAGGGTTTAAATGGCACCCAGCAGTTGGTGTGAGG");
446 string s1("AGCGGGAAGGGTTTAAATGGCACCGGGCAGTTGGCGTGAGG");
447 string s2("CAGCGCCGGGGTTTAAATGGCACCGAGCAGTTGGCGCAGGG");
450 analysis.append_sequence(s0);
451 analysis.append_sequence(s1);
452 analysis.append_sequence(s2);
453 analysis.set_threshold(23);
454 analysis.set_window(30);
456 NwayPaths npath = analysis.paths();
457 BOOST_CHECK_EQUAL( npath.refined_pathz.size(), 1 );
459 list<ConservedPath::path_type> result;
460 list<vector<bool> > reversed;
461 // grab 1 path (since there's only one)
462 list<ConservedPath>::iterator pathz_i = npath.pathz.begin();
463 list<ConservedPath> selected_paths;
464 selected_paths.push_back(*pathz_i);
465 analysis.createLocalAlignment(selected_paths.begin(),
466 selected_paths.end(),
470 for(std::list<ConservedPath::path_type>::iterator result_i = result.begin();
471 result_i != result.end();
474 ConservedPath::path_element first_element = *(result_i->begin());
475 for (ConservedPath::path_type::iterator element_i = result_i->begin();
476 element_i != result_i->end();
479 BOOST_CHECK_EQUAL( *element_i, first_element );
480 BOOST_CHECK_EQUAL( s0[*element_i], s1[*element_i] );
481 BOOST_CHECK_EQUAL( s1[*element_i], s2[*element_i] );
482 BOOST_CHECK_EQUAL( s0[*element_i], s2[*element_i] );
487 BOOST_AUTO_TEST_CASE( mussa_window_larger_than_sequence )
489 string s0("AGCAGGG");
490 string s1("CAGCGGG");
493 analysis.append_sequence(s0);
494 analysis.append_sequence(s1);
495 analysis.set_threshold(23);
496 analysis.set_window(30);
497 BOOST_CHECK_THROW(analysis.analyze(), seqcomp_error);
500 BOOST_AUTO_TEST_CASE( subanalysis )
502 Sequence s1("AATGAAGATTTTAATGCTTTAATTTTGTTTTGTAAACTTCGAATTTCCAAAATTTGAAA");
503 Sequence s2("AGGAGCAAGTTCGCTTCATCGAGAATTTTTAATTTTTAGTCAAATTTTCCAATGTCTGA");
506 analysis.append_sequence(s1);
507 analysis.append_sequence(s2);
508 analysis.set_threshold(8);
509 analysis.set_window(8);
512 NwayPaths perfect_path = analysis.paths();
513 int perfect_match_count = perfect_path.pathz.size();
515 Sequence sub1 = s1.subseq(2, s1.size()-4);
516 Sequence sub2 = s2.subseq(2, s2.size()-4);
518 subanalysis.append_sequence(sub1);
519 subanalysis.append_sequence(sub2);
520 subanalysis.set_threshold(7);
521 subanalysis.set_window(8);
522 subanalysis.analyze();
523 NwayPaths one_mismatch_path = subanalysis.paths();
524 int one_mismatch_count = one_mismatch_path.pathz.size();
526 BOOST_CHECK( perfect_match_count < one_mismatch_count );
529 BOOST_AUTO_TEST_CASE( dirty_flag )
532 BOOST_CHECK_EQUAL(m.is_dirty(), false);
534 BOOST_CHECK_EQUAL(m.is_dirty(), true);
537 BOOST_CHECK_EQUAL(m.is_dirty(), true);
540 BOOST_CHECK_EQUAL(m.is_dirty(), true);
542 m.set_soft_threshold(1);
543 BOOST_CHECK_EQUAL(m.is_dirty(), false);
545 m.append_sequence("AAGGCCTT");
546 BOOST_CHECK_EQUAL(m.is_dirty(), true);