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