File: SaveSeqFile.cpp

package info (click to toggle)
clustalx 2.1%2Blgpl-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,320 kB
  • sloc: cpp: 40,050; sh: 163; xml: 102; makefile: 16
file content (429 lines) | stat: -rw-r--r-- 12,390 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * Changes:
 * Mark: 18-01-2007. Changed from using QFileDialog to FileDialog.
 *
 * 12-03-07,Nigel Brown(EMBL): fixed range selection (bz0029) in accept(),
 * which was miscalculating the end point of the selection. Dialog no longer
 * exits and outputs default range when given bad range values; changed
 * behaviour to allow user to correct input mistake and added more specific
 * textbox warnings. Also added check that user has selected at least one
 * output format and added corresponding textbox warning.
 *
 * 12-4-07, Mark Larkin, Removed destructor. No need to delete QObjects. 
 */
#include <QPushButton>
#include <QGroupBox>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QSizePolicy>
#include <QComboBox>
#include <QtWidgets>
#include <QLabel>
#include "SaveSeqFile.h"
#include "clustalW/general/userparams.h"
#include "clustalW/general/utils.h"
#include "clustalW/alignment/AlignmentOutput.h"
#include "FileDialog.h"

SaveSeqFile::SaveSeqFile(QString inputFileName, int _type, rangeParams params)
 : type(_type)
{
    clustalw::userParameters->setRangeFromToSet(false);
    clustalw::userParameters->setSeqRange(false);
    firstSeq = params.firstSeq;
    lastSeq = params.lastSeq;
    firstRes = params.firstRes;
    lastRes = params.lastRes;

    if(type == Sequences)
    {
        title = "SAVE SEQUENCES";
        label = "Save Sequences As: (File extension will be appended)";    
    }
    else if(type == Profile1)
    {
        title = "SAVE PROFILE";
        label = "Save Profile 1 As: (File extension will be appended)";    
    }
    else if(type == Profile2)
    {
        title = "SAVE PROFILE";
        label = "Save Profile 2 As: (File extension will be appended)";    
    }
    else
    {
        title = "";
        label = "";
    }

    if(inputFileName.size() > 0)
    {
        std::string path = "";
        clustalw::utilityObject->getPath(inputFileName.toStdString(), &path);
        
        if(path.size() > 0)
        {
            fileName = QString(path.c_str());
            int len = fileName.size();
            if(fileName[len -1] == '.')
            {
                fileName[len -1] = ' ';
                fileName = fileName.trimmed();
            }
        }
        else
        {
           fileName = QDir::currentPath() + QString(QDir::separator()) + "temp";
        }
    }    
    fileGridBox = new QGroupBox("Format");
    otherParams = new QGroupBox;
    
    mainLayout = new QVBoxLayout;
     
    okButton = new QPushButton(tr("OK"));
    okButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
    cancelButton = new QPushButton(tr("Cancel"));
    cancelButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel())); 
    browseButton = new QPushButton(tr("Browse"));
    browseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(browseButton, SIGNAL(clicked()), this, SLOT(browse()));        
    setUpLayout();
    
    mainLayout->addWidget(fileGridBox);
    mainLayout->addWidget(otherParams);
    setLayout(mainLayout);
}

void SaveSeqFile::setUpLayout()
{
   setUpRadioButtons(); 
   grid = new QGridLayout; 
   grid->addWidget(clustal, 0, 0);
   grid->addWidget(nbrf, 0, 1);
   grid->addWidget(gcg, 1, 0);
   grid->addWidget(phylip, 1, 1);
   grid->addWidget(gde, 2, 0);
   grid->addWidget(nexus, 2, 1);
   grid->addWidget(fasta, 3, 0);
   fileGridBox->setLayout(grid);
   
   setUpOtherParams();
   otherParamLayout = new QGridLayout; 
   otherParamLayout->addWidget(gdeOutputCaseLabel, 0, 0);
   otherParamLayout->addWidget(gdeOutputCase, 0, 1);
   otherParamLayout->addWidget(clustalSequenceNumbersLabel, 1, 0);
   otherParamLayout->addWidget(clustalSequenceNumbers, 1, 1);
   otherParamLayout->addWidget(useRangeInfo, 2, 0);
   otherParamLayout->addWidget(rangeFromLabel, 3, 0);
   otherParamLayout->addWidget(rangeFrom, 3, 1);
   otherParamLayout->addWidget(rangeToLabel, 3, 2);
   otherParamLayout->addWidget(rangeTo, 3, 3);
   otherParamLayout->addWidget(rangeNumbersLabel, 4, 0);
   otherParamLayout->addWidget(rangeNumbers, 4, 1);
   otherParamLayout->addWidget(saveAsLabel, 5, 0);
   otherParamLayout->addWidget(saveAs, 6, 0, 1, 3);
   otherParamLayout->addWidget(browseButton, 6, 4);
   otherParamLayout->addWidget(okButton, 7, 0);
   otherParamLayout->addWidget(cancelButton, 7, 1);
   otherParams->setLayout(otherParamLayout); 
}

void SaveSeqFile::setUpRadioButtons()
{
    clustal = new QCheckBox("CLUSTAL format"); 
    gcg = new QCheckBox("GCG/MSF format");
    gde = new QCheckBox("GDE format");
    fasta = new QCheckBox("FASTA format");
    nbrf = new QCheckBox("NBRF/PIR format");
    phylip = new QCheckBox("PHYLIP format");
    nexus = new QCheckBox("NEXUS format");
        
    if(clustalw::userParameters->getOutputClustal())
    {
        clustal->setChecked(true);
    }
    if(clustalw::userParameters->getOutputGCG())
    {
        gcg->setChecked(true);
    }
    if(clustalw::userParameters->getOutputGde())
    {
        gde->setChecked(true);
    }
    if(clustalw::userParameters->getOutputFasta())
    {
        fasta->setChecked(true);
    }
    if(clustalw::userParameters->getOutputNbrf())
    {
        nbrf->setChecked(true);
    }
    if(clustalw::userParameters->getOutputPhylip())
    {
        phylip->setChecked(true);
    }  
    if(clustalw::userParameters->getOutputNexus())
    {
        nexus->setChecked(true);
    }             
}

void SaveSeqFile::setUpOtherParams()
{
    gdeOutputCase = new QComboBox();
    gdeOutputCase->addItem("Lower");    
    gdeOutputCase->addItem("Upper");
    if(clustalw::userParameters->getLowercase())
    {
        gdeOutputCase->setCurrentIndex(0);
    }
    else
    {
        gdeOutputCase->setCurrentIndex(1);
    }
    gdeOutputCaseLabel = new QLabel(tr("GDE output case :"));
    
    clustalSequenceNumbers = new QComboBox();
    clustalSequenceNumbers->addItem("Off");
    clustalSequenceNumbers->addItem("On");
    if(clustalw::userParameters->getClSeqNumbers())
    {
        clustalSequenceNumbers->setCurrentIndex(1);
    }
    else
    {
        clustalSequenceNumbers->setCurrentIndex(0);
    }
    clustalSequenceNumbersLabel = new QLabel(tr("CLUSTALW sequence numbers :"));
    
    
    rangeNumbers = new QComboBox();
    rangeNumbers->addItem("Off");
    rangeNumbers->addItem("On");
    if(clustalw::userParameters->getSeqRange())
    {
        clustalSequenceNumbers->setCurrentIndex(1);
    }
    else
    {
        clustalSequenceNumbers->setCurrentIndex(0);
    }
            
    useRangeInfo = new QCheckBox("Use range information: ");
    useRangeInfo->setChecked(false);
	if(!clustalw::userParameters->getRangeFromToSet())
    {
		useRangeInfo->setChecked(false);
	}
	else
	{
	    useRangeInfo->setChecked(true);
	}
    connect(useRangeInfo, SIGNAL(clicked()), this, SLOT(changeUseRange()));
     
    rangeFrom = new QLineEdit;
    rangeTo = new QLineEdit;
    
    QString num;
    num = num.setNum(firstRes);
    rangeFrom->setText(num);
    
    num = num.setNum(lastRes);
    rangeTo->setText(num);
    
    rangeFromLabel = new QLabel(tr("Save range from: "));
    rangeToLabel = new QLabel(tr(" to: "));
    rangeNumbersLabel = new QLabel(tr("Include range numbers: ")); 
    
    saveAsLabel = new QLabel(label);
    saveAs = new QLineEdit;
    saveAs->setText(fileName);
    changeUseRange();
}

void SaveSeqFile::changeUseRange()
{
    if(useRangeInfo->isChecked())
    {
        // Enable
        rangeFrom->setEnabled(true);
        rangeTo->setEnabled(true);
        rangeNumbers->setEnabled(true);
    }
    else
    {
        // disable
        rangeFrom->setEnabled(false);
        rangeTo->setEnabled(false);
        rangeNumbers->setEnabled(false);
    }
}

void SaveSeqFile::browse()
{
    QString myFile;
                                   
    // Mark Jan 18th 2007: changes to remember working Dir    
    FileDialog fd;
    myFile = fd.getSaveFileName(this, tr("Save as"));
                                       
    if(myFile != "")
    {
        saveAs->setText(myFile);
    }
}

void SaveSeqFile::cancel()
{
    setResult(QDialog::Rejected);
    close();
}

void SaveSeqFile::accept()
{
    // Find out which ones (and how many [nige]) have been checked!
    int selected = 0;

    /*
      If 'userParameters::setOutputWhatever(bool)' returned boolean, this
      would be 1/6 as long:
      
      selected += clustalw::userParameters->setOutputClustal(clustal->isChecked());
      selected += clustalw::userParameters->setOutputGCG(gcg->isChecked());
      ...
    */

    if (clustal->isChecked()) {
        clustalw::userParameters->setOutputClustal(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputClustal(false);
    }
    if (gcg->isChecked()) {
        clustalw::userParameters->setOutputGCG(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputGCG(false);
    }
    if (gde->isChecked()) {
        clustalw::userParameters->setOutputGde(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputGde(false);
    }
    if (fasta->isChecked()) {
        clustalw::userParameters->setOutputFasta(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputFasta(false);
    }
    if (nbrf->isChecked()) {
        clustalw::userParameters->setOutputNbrf(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputNbrf(false);
    }
    if (phylip->isChecked()) {
        clustalw::userParameters->setOutputPhylip(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputPhylip(false);
    }
    if (nexus->isChecked()) {
        clustalw::userParameters->setOutputNexus(true);
        selected++;
    } else {
        clustalw::userParameters->setOutputNexus(false);
    }

    if (! selected) {
        QMessageBox::information(this, "", "Please select at least one output format.",
                                 QMessageBox::Ok);
        return;
    }
    
    // if range is selected
    // Check the range is ok. If it isn't use the default
    if (useRangeInfo->isChecked())
    {
        if (rangeNumbers->currentIndex() == 0)
        {
            clustalw::userParameters->setRangeFromToSet(false);
            clustalw::userParameters->setSeqRange(false);
        }
        else
        {
            clustalw::userParameters->setRangeFromToSet(true);
            clustalw::userParameters->setSeqRange(true);
        }
            
        bool ok1;
        QString textFrom = rangeFrom->text();
        int numFrom = textFrom.toInt(&ok1);
            
        bool ok2;
        QString textTo = rangeTo->text();
        int numTo = textTo.toInt(&ok2);
        
        if (!ok1 || !ok2) {
            QMessageBox::information(this, "", "Range values must be integers.",
                                     QMessageBox::Ok);
            return;
        }
        
        if (numFrom < firstRes || numFrom > lastRes ||
            numTo   < firstRes || numTo   > lastRes) {
            QMessageBox::information(this, "", "Invalid range.",
                                     QMessageBox::Ok);
            return;
        }

        if (numTo < numFrom) {
            QMessageBox::information(this, "", "Invalid range.",
                                     QMessageBox::Ok);
            return;
        }

        clustalw::userParameters->setRangeFrom(numFrom);
        clustalw::userParameters->setRangeTo(numTo);
    }

    // GDE output case
    if(gdeOutputCase->currentIndex() == 0)
    {
        clustalw::userParameters->setLowercase(true);
    }
    else
    {
        clustalw::userParameters->setLowercase(false);
    }
        
    // Clustalw sequence numbers
    if(clustalSequenceNumbers->currentIndex() == 1)
    {
        clustalw::userParameters->setClSeqNumbers(true);
    }
    else
    {
        clustalw::userParameters->setClSeqNumbers(false);
    }
        
    // save sequences as
    fileName = saveAs->text();
    int len = fileName.size();
    if(fileName == "")
    {
        fileName = QDir::currentPath() + "temp.aln";
    }
    else if(fileName[len -1] != '.')
    {
        fileName += ".";
    }
    
    setResult(QDialog::Accepted);
    hide();         
}