use spinboxes for the setup analysis dialog
[mussa.git] / qui / mussa_setup_dialog / MussaSetupWidget.cpp
1 #include <QFrame>
2 #include <QLabel>
3 #include <QLineEdit>
4 #include <QSpinBox>
5 #include <QVBoxLayout>
6 #include <QHBoxLayout>
7 #include <QPushButton>
8
9 #include <list>
10
11 using namespace std;
12
13 #include <boost/filesystem/path.hpp>
14 namespace fs = boost::filesystem;
15
16 //#include "qui/mussa_setup_dialog/SequenceSetupWidget.hpp"
17 #include "qui/mussa_setup_dialog/SequenceSetupFrame.hpp"
18 #include "qui/mussa_setup_dialog/MussaSetupWidget.hpp"
19 #include "qui/mussa_setup_dialog/SetupInfo.hpp"
20 #include "alg/mussa.hpp"
21 #include "mussa_exceptions.hpp"
22
23 MussaSetupWidget::MussaSetupWidget(QWidget *parent)
24   : QWidget(parent),
25     analysisNameLineEdit(new QLineEdit),
26     windowEdit(new QSpinBox),
27     thresholdEdit(new QSpinBox),
28     numOfSequencesSpinBox(new QSpinBox),
29     createPushButton(new QPushButton(tr("Create"))),
30     cancelPushButton(new QPushButton(tr("Cancel"))),
31     seqSetupFrame(0)
32 {
33   
34   // Analysis name
35   QLabel *analysisNameLabel = new QLabel(tr("Analysis Name"));
36   analysisNameLabel->setBuddy(analysisNameLineEdit);
37
38   // Window
39   windowEdit->setMinimum(1);
40   windowEdit->setValue(30);
41   QLabel *windowLabel = new QLabel(tr("Window (nt)"));
42   windowLabel->setBuddy(windowEdit);
43
44   // Threshold
45   thresholdEdit->setMinimum(1);
46   thresholdEdit->setMaximum(windowEdit->value());
47   thresholdEdit->setValue(21);
48   QLabel *thresholdLabel = new QLabel(tr("Threshold (nt)"));
49   thresholdLabel->setBuddy(thresholdEdit);
50   connect(windowEdit, SIGNAL(valueChanged(int)),
51           this, SLOT(updateThreshold(int)));
52
53   // Number of sequences
54   QLabel *numOfSequencesLabel = new QLabel(tr("Number of sequences"));
55   numOfSequencesLabel->setBuddy(numOfSequencesSpinBox);
56   numOfSequencesSpinBox->setMinimum(1);
57
58   //Sequence setup frame
59   seqSetupFrame = new SequenceSetupFrame;
60
61   connect(numOfSequencesSpinBox, SIGNAL(valueChanged(int)),
62           seqSetupFrame, SLOT(changeSequenceCount(int)));
63
64   numOfSequencesSpinBox->setValue(2);
65
66   // Create Experiment Button
67   connect(createPushButton, SIGNAL(pressed()),
68           this, SLOT(mussaCreatePushed()));
69   
70   // Cancle Button
71   connect(cancelPushButton, SIGNAL(pressed()),
72           this, SLOT(mussaCancelPushed()));
73
74   // LAYOUT
75   QHBoxLayout *row1Layout = new QHBoxLayout;
76   QHBoxLayout *row2Layout = new QHBoxLayout;
77   QVBoxLayout *mainLayout = new QVBoxLayout;
78   QHBoxLayout *buttonLayout = new QHBoxLayout;
79
80   row1Layout->addWidget(analysisNameLabel);
81   row1Layout->addWidget(analysisNameLineEdit);
82
83   row2Layout->addWidget(thresholdLabel);
84   row2Layout->addWidget(thresholdEdit);
85   row2Layout->addWidget(windowLabel);
86   row2Layout->addWidget(windowEdit);
87   row2Layout->addWidget(numOfSequencesLabel);
88   row2Layout->addWidget(numOfSequencesSpinBox);
89
90   buttonLayout->addStretch(1);
91   buttonLayout->addWidget(createPushButton);
92   buttonLayout->addWidget(cancelPushButton);
93
94   mainLayout->addLayout(row1Layout);
95   mainLayout->addLayout(row2Layout);
96   mainLayout->addWidget(seqSetupFrame);
97   mainLayout->addLayout(buttonLayout);
98   setLayout(mainLayout);
99 }
100
101 void MussaSetupWidget::mussaCreatePushed()
102 {
103   emit createButtonPushed();
104 }
105
106 void MussaSetupWidget::mussaCancelPushed()
107 {
108   emit cancelButtonPushed();
109 }
110
111 void MussaSetupWidget::updateThreshold(int new_threshold)
112 {
113   thresholdEdit->setMaximum(new_threshold);
114 }
115
116 MussaRef MussaSetupWidget::getMussaObject()
117 {
118   MussaRef mussa(new Mussa);
119
120   int fastaIndex;
121   int start;
122   int end;
123
124   list<SetupInfo *> setupInfoList = seqSetupFrame->getSetupInfo();
125   
126   for (list<SetupInfo *>::reverse_iterator setup_item = setupInfoList.rbegin();
127        setup_item != setupInfoList.rend();
128        ++setup_item)
129   {
130     std::string seqName = (*setup_item)->getName();
131     std::string seqNative = (*setup_item)->getSeqFile();
132     std::string annotNative = (*setup_item)->getAnnotFile();
133     fastaIndex = (*setup_item)->getFastaIndex();
134     start = (*setup_item)->getSubSeqStart();
135     end = (*setup_item)->getSubSeqEnd();
136   
137     fs::path seqFile(seqNative, fs::native);
138     fs::path annotFile(annotNative, fs::native);
139     mussa->load_sequence(seqFile, annotFile, fastaIndex, start, end, &seqName);
140   }
141   setupInfoList.clear();
142
143   int win_size = windowEdit->value();
144   int threshold = thresholdEdit->value();
145   std::string name = analysisNameLineEdit->text().toStdString();
146   if (win_size == 0 or threshold == 0) {
147     throw mussa_load_error("must set analysis parameters");
148   } else {
149     mussa->set_name(name);
150     mussa->set_window(win_size);
151     mussa->set_threshold(threshold);
152     mussa->set_analysis_mode(Mussa::TransitiveNway);
153     //mussa->set_entropy(0); // might want to add this at some point
154     mussa->analyze();
155   }
156
157   return mussa;
158 }
159