fix problems with motif changes not showing up in sequencebrowser
[mussa.git] / qui / motif_editor / test / TestMotifEditor.hpp
1 #ifndef TESTMOTIFEDITOR_HPP_
2 #define TESTMOTIFEDITOR_HPP_
3 #include <QtGui>
4 #include <QVariant>
5 #include <QtTest/QtTest>
6 #include <QtTest/QSignalSpy>
7
8 #include <vector>
9 #include "alg/sequence.hpp"
10
11 #include "qui/motif_editor/MotifEditor.hpp"
12 #include "qui/motif_editor/MotifModel.hpp"
13
14 class TestMotifEditor : public QObject
15 {
16   Q_OBJECT
17
18 private slots:
19
20   void testSimpleConstructor() {
21     MussaRef m1(new Mussa);
22     MotifEditor *editor = new MotifEditor(m1);
23     
24     QVERIFY(editor->get_analysis() == m1);
25     QVERIFY(editor->get_model() != 0);
26   }
27
28   // do we still have motifs in our sequences after hitting apply?
29   void testEditColorApply() {
30     std::vector<Sequence> motifs;
31     motifs.push_back("AAGG");
32     std::vector<Color> colors;
33     colors.push_back(Color(0.0, 0.0, 1.0));
34         
35     MussaRef m(new Mussa);
36     m->append_sequence("AAAAGGGGTTTT");
37     m->append_sequence("AAAAAAAACCCC");
38     m->set_motifs(motifs, colors);
39     
40     // do the sequences have the right motifs?
41     std::vector<SequenceRef> sequences1(m->sequences());
42     QVERIFY(sequences1[0]->motifs().size() == 1);
43     QVERIFY(sequences1[1]->motifs().size() == 0);
44     
45     // get color
46     boost::shared_ptr<AnnotationColors> cm(m->colorMapper());
47     QVERIFY(cm->lookup("motif", "AAGG") == Color(0.0, 0.0, 1.0));
48         
49     MotifEditor *editor(new MotifEditor(m));
50     MotifModel *model(editor->get_model());
51     // the AAGG motif and the emtpy motif
52     QVERIFY(model->size() == 2);
53     QModelIndex row0_color(model->index(0, MotifModel::ColorCell));
54     
55     model->setData(row0_color, QVariant(QColor(0,255,0)));
56     // this is equivalent to pressing apply
57     editor->updateAnalysisMotifs();
58
59     std::vector<SequenceRef> sequences2(m->sequences());
60     QVERIFY(sequences2[0]->motifs().size() == 1);
61     QVERIFY(sequences2[1]->motifs().size() == 0);
62     QVERIFY(cm->lookup("motif", "AAGG") == Color(0.0, 1.0, 0.0));
63     QVERIFY(m->colorMapper()->lookup("motif", "AAGG") == Color(0.0, 1.0, 0.0));
64     
65     QVERIFY(editor->get_analysis() == m);
66     QVERIFY(editor->get_model() == model);
67     QVERIFY(m->colorMapper() == cm);
68   }
69
70   void testEditMotifApply() {
71     std::vector<Sequence> motifs;
72     motifs.push_back("AAGG");
73     std::vector<Color> colors;
74     colors.push_back(Color(0.0, 0.0, 1.0));
75         
76     MussaRef m(new Mussa);
77     m->append_sequence("AAAAGGGG");
78     m->append_sequence("AAAACCCC");
79     m->set_motifs(motifs, colors);
80     
81     // do the sequences have the right motifs?
82     std::vector<SequenceRef> sequences1(m->sequences());
83     QVERIFY(sequences1[0]->get_sequence() == "AAAAGGGG");
84     QVERIFY(sequences1[1]->get_sequence() == "AAAACCCC");
85     QVERIFY(sequences1[0]->motifs().size() == 1);
86     QVERIFY(sequences1[1]->motifs().size() == 0);
87     
88     // get color
89     boost::shared_ptr<AnnotationColors> cm(m->colorMapper());
90     QVERIFY(cm->lookup("motif", "AAGG") == Color(0.0, 0.0, 1.0));
91         
92     MotifEditor *editor(new MotifEditor(m));
93     MotifModel *model(editor->get_model());
94     // the AATT motif and the empty motif
95     QVERIFY(model->size() == 2);
96     QModelIndex row0_color(model->index(0, MotifModel::ColorCell));
97     QModelIndex row0_sequence(model->index(0, MotifModel::SequenceCell));
98     QVERIFY(model->data(row0_sequence) == "AAGG");
99     
100     // change contents of cell
101     model->setData(row0_sequence, QVariant("AACC"));
102     // this is equivalent to pressing apply
103     editor->updateAnalysisMotifs();
104     QVERIFY(model->data(row0_sequence) == "AACC");
105
106     // does the mussa analysis have the right data?
107     std::vector<SequenceRef> sequences2(m->sequences());
108     QVERIFY(sequences2[0]->motifs().size() == 0);
109     QVERIFY(sequences2[1]->motifs().size() == 1);
110     QVERIFY(cm->lookup("motif", "AACC") == Color(0.0, 0.0, 1.0));
111     QVERIFY(m->colorMapper()->lookup("motif", "AACC") == Color(0.0, 0.0, 1.0));
112     QVERIFY(cm->lookup("motif", "AAGG") == Color(0.0, 0.0, 0.0));
113     QVERIFY(m->colorMapper()->lookup("motif", "AAGG") == Color(0.0, 0.0, 0.0));
114     
115     QVERIFY(editor->get_analysis() == m);
116     QVERIFY(editor->get_model() == model);
117     QVERIFY(m->colorMapper() == cm);
118   }
119 /*
120   
121 public slots:
122   void updateModel();
123   void updateAnalysisMotifs();
124
125 signals:
126   void changedMotifs();
127   */
128 };
129 #endif /*TESTMOTIFEDITOR_HPP_*/