load and display a motif list
[mussa.git] / alg / test / test_sequence.cpp
1 #include <boost/test/auto_unit_test.hpp>
2 #include <list>
3 #include <iostream>
4
5 #include "alg/sequence.hpp"
6 #include "mussa_exceptions.hpp"
7
8 using namespace std;
9
10 //! when we try to load a missing file, do we get an error?
11 BOOST_AUTO_TEST_CASE( sequence_load_exception )
12 {
13   Sequence s;
14   // there should be errors when we try to load something that doesn't exist
15   BOOST_CHECK_THROW( s.load_fasta("alkejralk", 1, 0, 0), mussa_load_error);
16   BOOST_CHECK_THROW( s.load_annot("alkejralk", 0, 0), mussa_load_error);
17 }
18
19 //! Do simple operations work correctly?
20 BOOST_AUTO_TEST_CASE( sequence_filter )
21 {
22   Sequence s1("AATTGGCC");
23   BOOST_CHECK_EQUAL(s1.get_seq(), "AATTGGCC");
24
25   Sequence s2("aattggcc");
26   BOOST_CHECK_EQUAL(s2.get_seq(), "AATTGGCC");
27   BOOST_CHECK_EQUAL(s2.rev_comp(), "GGCCAATT");
28   BOOST_CHECK_EQUAL(s2.size(), s2.get_seq().size());
29   BOOST_CHECK_EQUAL(s2.c_seq(), s2.get_seq().c_str());
30
31   Sequence s3("asdfg");
32   BOOST_CHECK_EQUAL(s3.get_seq(), "ANNNG");
33   BOOST_CHECK_EQUAL(s3.subseq(0,2), "AN");
34
35   s3.set_filtered_sequence("AAGGCCTT", 0, 2); 
36   BOOST_CHECK_EQUAL(s3.get_seq(), "AA");
37   s3.set_filtered_sequence("AAGGCCTT", 2, 2);
38   BOOST_CHECK_EQUAL( s3.get_seq(), "GG");
39   s3.set_filtered_sequence("AAGGCCTT", 4);
40   BOOST_CHECK_EQUAL( s3.get_seq(), "CCTT");
41   
42   s3.clear();
43   BOOST_CHECK_EQUAL(s3.get_seq(), "");
44
45   s3.set_seq("AAGGFF");
46   BOOST_CHECK_EQUAL(s3.get_seq(), "AAGGNN");
47 }
48
49 //! Can we load data from a file
50 BOOST_AUTO_TEST_CASE( sequence_load )
51 {
52   Sequence s;
53   s.load_fasta("examples/seq/human_mck_pro.fa");
54   BOOST_CHECK_EQUAL(s.subseq(0, 5), "GGATC"); // first few chars of fasta file
55   BOOST_CHECK_EQUAL(s.get_header(), "gi|180579|gb|M21487.1|HUMCKMM1 Human "
56                                     "muscle creatine kinase gene (CKMM), "
57                                     "5' flank");
58 }
59
60 BOOST_AUTO_TEST_CASE ( sequence_empty )
61 {
62   Sequence s;
63   BOOST_CHECK_EQUAL( s.empty(), true );
64   s = "AAAGGG";
65   BOOST_CHECK_EQUAL( s.empty(), false );
66 }
67
68 BOOST_AUTO_TEST_CASE ( sequence_iterators )
69 {
70   std::string seq_string = "AAGGCCTTNNTATA";
71   Sequence s(seq_string);
72   const Sequence cs(s);
73   std::string::size_type count = 0;
74
75   std::string::iterator str_itor;
76   Sequence::iterator s_itor;
77   Sequence::const_iterator cs_itor;
78
79   for( str_itor = seq_string.begin(),
80        s_itor   = s.begin(),
81        cs_itor  = cs.begin();
82        str_itor != seq_string.end() and
83        s_itor   != s.end() and
84        cs_itor  != cs.end();
85        ++str_itor, ++s_itor, ++cs_itor, ++count)
86   {
87     BOOST_CHECK_EQUAL ( *str_itor, *s_itor );
88     BOOST_CHECK_EQUAL ( *s_itor, *cs_itor );
89     BOOST_CHECK_EQUAL ( *cs_itor, *str_itor );
90   }
91   BOOST_CHECK_EQUAL( seq_string.size(), count );
92   BOOST_CHECK_EQUAL( s.size(), count );
93   BOOST_CHECK_EQUAL( cs.size(), count );
94 }
95
96 BOOST_AUTO_TEST_CASE( sequence_motifs )
97 {
98   string m("AAAA");
99   string bogus("AATTGGAA");
100   Sequence s1("AAAAGGGGCCCCTTTT");
101
102   list<motif>::const_iterator motif_i = s1.motifs().begin();
103   list<motif>::const_iterator motif_end = s1.motifs().end();
104
105   // do our iterators work?
106   BOOST_CHECK( motif_i == s1.motifs().begin() );
107   BOOST_CHECK( motif_end == s1.motifs().end() );
108   BOOST_CHECK( motif_i == motif_end );
109
110
111   s1.add_motif(bogus);
112   BOOST_CHECK( s1.motifs().begin() == s1.motifs().end() );
113   s1.add_motif(m);
114   BOOST_CHECK( s1.motifs().begin() != s1.motifs().end() );
115   BOOST_CHECK_EQUAL( s1.motifs().size(), 2 );
116
117   for(motif_i = s1.motifs().begin(); 
118       motif_i != s1.motifs().end(); 
119       ++motif_i)
120   {
121     BOOST_CHECK_EQUAL( motif_i->type, "motif" );
122     BOOST_CHECK_EQUAL( motif_i->name, m);
123     BOOST_CHECK_EQUAL( motif_i->sequence, m);
124   }
125
126   s1.clear_motifs();
127   BOOST_CHECK( s1.motifs().begin() == s1.motifs().end() );
128 }
129
130 BOOST_AUTO_TEST_CASE( annot_test )
131 {
132   annot a(0, 10, "test", "thing");
133
134   BOOST_CHECK_EQUAL( a.start, 0 );
135   BOOST_CHECK_EQUAL( a.end,   10 );
136   BOOST_CHECK_EQUAL( a.type,  "test" );
137   BOOST_CHECK_EQUAL( a.name,  "thing" );
138
139   motif m(10, "AAGGCC");
140   BOOST_CHECK_EQUAL( m.start, 10 );
141   BOOST_CHECK_EQUAL( m.type, "motif" );
142   BOOST_CHECK_EQUAL( m.name, "AAGGCC" );
143   BOOST_CHECK_EQUAL( m.end,  10+6 );
144 }