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