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