File: AlignmentViewerWidget.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 (795 lines) | stat: -rw-r--r-- 26,576 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/**
 * Changes:
 *
 * 16-01-07,Nigel Brown(EMBL): Added Q_MOS_MAC preprocessor checks for Mac OS
 * X port, which uses AlignmentWidgetScroll in place of AlignmentWidget, and
 * HistogramWidgetScroll in place of HistogramWidget. This is to work around a
 * scrolling limitation in Qt4 QScrollArea. THESE CHANGES SHOULD BE REMOVED
 * WHEN QT4 QSCROLLAREA IS FIXED! *
 * Andreas Wilm: fixed in Qt 4.3.0, see bug entry 
 * http://lists.trolltech.com/qt-interest/2007-01/thread00541-0.html
 * and
 * http://trolltech.org/developer/task-tracker/index_html?method=entry&id=147640
 *
 * Mark: 18-01-2007. Changed from using QFileDialog to FileDialog.
 * Mark: 19-01-2007. Changed the width of the boxes to make them narrower. Also
 * added some larger font sizes. updateFontAndBoxes is the function I changed.
 *
 * 30-01-07,Nigel Brown(EMBL): changed sizePolicy settings for
 * CLU_AVOID_MAC_SCROLL_BUG compiled histWidget and alignWidget. Changed
 * setSizePolicy() on alignWidget and histWidget.
 *
 * 31-01-07,Nigel Brown(EMBL): added defaultFontSize member. Changed
 * updateFontAndBoxes() to use OS_MAC from compile.h to set OS-specific
 * box sizes: Mac boxes now much tighter vertically around characters (ugly -
 * not portable). Removed default case, setting locals at outset.
 *
 * 03-02-07,Nigel Brown(EMBL): changed outputColumnScores() to call
 * FileDialog::getSaveDataFileName() in place of
 * FileDialog::getSaveFileName(); the new version supplies a file suffix hint,
 * which is 'qscores' as used in clustalX. Disabled "Error: Bad name format"
 * message box in outputColumnScores() since this occurred on 'Cancel'
 * action. Added 'All Files (*)' selection.
 *
 * 16-02-07,Nigel Brown(EMBL): Changed font to Courier New; made boxwidth,
 * boxsize automatically derive from font size; added more font sizes. Changed
 * parameter order on AlignmentWidget::changeBoxSizeAndFontSize().
 *
 * 20-02-07,Nigel Brown(EMBL): Changed SeqNames size policy to expanding;
 * installed event filter on namesScroll in createGridBox(); small changes to
 * createGridBox() layout; hasSeqNameWidgetFocus() tests both the QScrollarea
 * and, if necessary, the child SeqNameWidget for focus. The latter was needed
 * after changing SeqNameWidget resizing and event handling.
 *
 * 15-03-07,Nigel Brown(EMBL): added font sizes to include classical
 * typographic scalings.
 *
 * 12-4-07, Mark Larkin : Changed destructor. We dont need to delete QObjects.
 *
 * 04-05-07,Nigel Brown(EMBL): hasSeqNameWidgetFocus() now tests all children
 * for focus; renamed hasSeqNameWidgetFocus() to hasFocus().
 *
 * 09-05-07,Nigel Brown(EMBL): Added calls in createGridBox() to new
 * AlignmentWidget::registerScrollArea() and
 * SeqNameWidget::registerScrollArea(), allowing each instance to know its own
 * containing QScrollArea (This is not the same as parentWidget()).
 *
 * 27-06-07,Nigel Brown(EMBL): For screen layout balancing: added
 * recvNumSequencesChanged() and balanceNumSequences() slots, and
 * numSequencesChanged() signal.
 *****************************************************************************/


#include <QtWidgets>
#include <QFile>
#include <QTextStream>
#include "AlignmentViewerWidget.h"
#include "SaveSeqFile.h"
#include "clustalW/alignment/AlignmentOutput.h"
#include "PSPrinter.h"
#include "FileDialog.h"
#include <QFontMetrics>

AlignmentViewerWidget::AlignmentViewerWidget(QWidget *parent)
 : QWidget(parent),
   namesScroll(0),
   profileNum(0),
   nHead(0)
{
    firstSeq = 0; 
    lastSeq = 0;
    alignmentToDisplay = 0;
    defaultFontSize = 10;

    /*
     * 9 out of 10 Goldilocks prefer 'Courier New'
     *
     * nige (with apologies to the three bears)
     **/
    //displayFont = new QFont("Helvetica"); //too chunky
    //displayFont = new QFont("Courier");   //too heavy
    displayFont = new QFont("Courier New"); //just right
    if (! displayFont->exactMatch())
        displayFont->setFamily("Courier");  //heavier
    displayFont->setPixelSize(defaultFontSize);

    QFontMetrics fontMetrics(*displayFont);
    QSize size = fontMetrics.size(Qt::TextSingleLine, QString("W"));
    defaultBoxWidth = size.width();
    defaultBoxSize  = size.height();
    //cout << defaultFontSize << " @ " << defaultBoxWidth << " x " << defaultBoxSize << endl;

    exRes = new clustalw::Array2D<int>();
    
    alignWidget = new AlignmentWidget(defaultBoxWidth, defaultBoxSize, displayFont);
    alignWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);
    histWidget = new HistogramWidget(defaultBoxWidth, defaultBoxSize);
    histWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);

    seqNames = new SeqNameWidget(defaultBoxSize, displayFont);
    //seqNames->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding); //- nige
    seqNames->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createGridBox();
    
    /*
     * create a layout for this widget.
     */
    mainlayout = new QVBoxLayout;
    mainlayout->addWidget(gridGroupBox);
    setLayout(mainlayout);
    
    updateFontAndBoxes("10");    
}

AlignmentViewerWidget::~AlignmentViewerWidget()
{
    delete exRes; // Mark 12-4-07, changed destructor.
}

void AlignmentViewerWidget::setAlignmentView(clustalw::Alignment* alignPtr, int fSeq, 
                                             int lSeq)
{
    alignmentToDisplay = alignPtr;
    firstSeq = fSeq;
    lastSeq = lSeq;
    sendInfoToAlignWidget();
    sendInfoToNameWidget();
}

void AlignmentViewerWidget::updateView()
{
    nHead = makeSecStructData();
    if(nHead == 0)
    {
        nHead = makeGapPenaltyData();
    }
        
    seqNames->updateDisplay(firstSeq, lastSeq, nHead, ssName);
    
    updateHistogram(firstSeq, lastSeq);
    alignWidget->updateDisplay(firstSeq, lastSeq, exRes, nHead, secStructData);
    moveNamesScrollBarToRight();//Mark 20-07-07
}

void AlignmentViewerWidget::updateView(int fSeq, int lSeq)
{
    firstSeq = fSeq;
    lastSeq = lSeq; 
    updateView();   
}

int AlignmentViewerWidget::getLengthLongestSeqInRange()
{
    return alignmentToDisplay->getLengthLongestSequence(firstSeq, lastSeq);
}

void AlignmentViewerWidget::showLowScoringSegments(bool show, 
                                                clustalw::Array2D<int>* _lowScoreRes)
{
    lowScoreRes = _lowScoreRes;
    alignWidget->showLowScoreSeg(show, lowScoreRes);
}

void AlignmentViewerWidget::showExceptionalRes(bool show)
{
    alignWidget->showExceptionalResidues(show);
}

void AlignmentViewerWidget::updateHistogram(int fSeq, int lSeq)
{
    int numSeqs = lastSeq - firstSeq + 1;
    if(fSeq != 0 && lSeq != 0)
    {
        exRes->clearArray();
        exRes->ResizeRect(numSeqs, 
                        alignmentToDisplay->getLengthLongestSequence(firstSeq, lastSeq));
        histogram = *(alignmentToDisplay->QTcalcHistColumnHeights(firstSeq - 1, numSeqs,
                                                                exRes));
        histWidget->addHistogramInfo(&histogram);
    }
    else
    {
        histWidget->clearHistogram();
    }
}

void AlignmentViewerWidget::updateFontAndBoxes(const QString& font)
{
    int newFontSize = font.toUInt();

    switch (newFontSize) {
    case 72: //-
    case 60: //-
    case 48: //-
    case 36: //-
    case 30:
    case 28:
    case 26:
    case 24:
    case 22:
    case 21: //-
    case 20:
    case 18:
    case 16:
    case 14:
    case 12:
    case 11:
    case 10:
    case  9:
    case  8:
        break;
    default:
        newFontSize = defaultFontSize;
    }

    displayFont->setPixelSize(newFontSize);

    QFontMetrics fontMetrics(*displayFont);
    QSize size = fontMetrics.size(Qt::TextSingleLine, QString("W"));
    int newBoxWidth = size.width();
    int newBoxSize  = size.height();
    //cout << newFontSize << " @ " << newBoxWidth << " x " << newBoxSize << endl;

    histWidget->updateBoxSize(newBoxWidth, newBoxSize);
    alignmentScroll->setVisible(false);
    alignWidget->changeBoxSizeAndFontSize(newBoxWidth, newBoxSize, displayFont);
    alignmentScroll->setVisible(true);
    seqNames->changeBoxSizeAndFontSize(newBoxSize, displayFont);
    repaint();
}

void AlignmentViewerWidget::createGridBox()
{
    /* 
     * Create a grid group box for the alignment viewer part 
     */
    gridGroupBox = new QGroupBox();
    layoutGridBox = new QGridLayout; 

    alignmentScroll = new QScrollArea;
    alignmentScroll->setWidget(alignWidget);
    alignWidget->registerScrollArea(alignmentScroll); //- nige

    conservationScroll = new QScrollArea;
    conservationScroll->setWidget(histWidget);
    conservationScroll->setFixedHeight(histWidget->height());

    alignmentScroll->setBackgroundRole(QPalette::Base);
    alignmentScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    conservationScroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    conservationScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    conservationScroll->setBackgroundRole(QPalette::Base);
    
    namesScroll = new QScrollArea;
    namesScroll->setWidget(seqNames);
    namesScroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    namesScroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    namesScroll->setBackgroundRole(QPalette::Base);

    /**
     * 20-02-07,Nigel: child intercepts resize events to adjust own size.
     */
    namesScroll->viewport()->installEventFilter(seqNames);
    seqNames->registerScrollArea(namesScroll); //- nige

    QScrollBar* alignHorizontalScroll = alignmentScroll->horizontalScrollBar();
    QScrollBar* conserveHorizontalScroll = conservationScroll->horizontalScrollBar();
    QScrollBar* alignVerticalScroll = alignmentScroll->verticalScrollBar();
    QScrollBar* namesVerticalScroll = namesScroll->verticalScrollBar();

    connect(alignHorizontalScroll, SIGNAL(valueChanged(int)), conserveHorizontalScroll,
            SLOT(setValue(int)));
    connect(alignVerticalScroll, SIGNAL(valueChanged(int)), namesVerticalScroll,
            SLOT(setValue(int)));
    connect(namesVerticalScroll, SIGNAL(valueChanged(int)), alignVerticalScroll,
            SLOT(setValue(int)));

    /*
     * Set the layout of the widgets.
     */
    layoutGridBox->addWidget(namesScroll,        0, 0, 1, 1);
    layoutGridBox->addWidget(alignmentScroll,    0, 1, 1, 5);
    layoutGridBox->addWidget(conservationScroll, 1, 1, 1, 5);

    /*
     * Set stretch and size policies.
     */
    layoutGridBox->setRowStretch(0, 5);
    layoutGridBox->setRowStretch(1, 0);
    layoutGridBox->setColumnStretch(0, 1);
    layoutGridBox->setColumnStretch(1, 5);

    //layoutGridBox->setRowStretch(0, 20);
    //layoutGridBox->setRowStretch(2, 0);
    //layoutGridBox->setColumnStretch(0, 1);
    //layoutGridBox->setColumnStretch(2, 3);

    gridGroupBox->setLayout(layoutGridBox);
    gridGroupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
}

void AlignmentViewerWidget::setColorScheme(ColorParameters* colors)
{
    alignWidget->setColorScheme(colors);
}    

void AlignmentViewerWidget::setPrintoutColorScheme(ColorParameters colors)
{
    alignWidget->setPrintoutColorScheme(colors);
}

void AlignmentViewerWidget::setBlackAndWhite()
{
    alignWidget->setBlackAndWhite();
}

void AlignmentViewerWidget::setBackGroundColoring(bool backgroundColor)
{
    alignWidget->setBackGroundColoring(backgroundColor);
}

/**
 * The function getAlignmentScrollBar is used to return the horizontal scrollbar from
 * the alignment scroll area. This is to allow the scroll lock in the profile viewing 
 * mode.
 * @return Pointer to the horizontal scroll bar in alignment scroll area.
 */
QScrollBar* AlignmentViewerWidget::getAlignmentScrollBar()
{
    return alignmentScroll->horizontalScrollBar();
}

void AlignmentViewerWidget::sendInfoToAlignWidget()
{
    alignWidget->setAlignment(alignmentToDisplay->getSeqArray(), alignmentToDisplay);
}

void AlignmentViewerWidget::sendInfoToNameWidget()
{
    seqNames->setNames(alignmentToDisplay);
}

void AlignmentViewerWidget::clearSequenceSelection()
{
    seqNames->clearSequenceSelection();
}

void AlignmentViewerWidget::clearRangeSelection()
{
    alignWidget->clearRangeSelection();
}

void AlignmentViewerWidget::selectAllSequences()
{
    seqNames->selectAllSequences();
}

void AlignmentViewerWidget::saveSequences(QString inputFileName, int type)
{
    if(firstSeq > 0 && lastSeq >= firstSeq)
    {
        int lastRes = alignmentToDisplay->getLengthLongestSequence(firstSeq, lastSeq);
    
        rangeParams alignmentRange;
        alignmentRange.firstSeq = firstSeq;
        alignmentRange.lastSeq = lastSeq;
        alignmentRange.firstRes = 1;
        alignmentRange.lastRes = lastRes;
        int code;
        SaveSeqFile saveSeqs(inputFileName, type, alignmentRange);
        code = saveSeqs.exec();
    
        if(code == QDialog::Accepted)
        {
            // save sequences as
            QString saveFileName = saveSeqs.getFileName();
            if(saveFileName.size() > 0)
            {
                // Save the file!!
                clustalw::AlignmentOutput alignmentOutput;
                alignmentOutput.openAlignmentOutput(saveFileName.toStdString());
                alignmentOutput.createAlignmentOutput(alignmentToDisplay, firstSeq, lastSeq);
            }
            else
            {
                QString tempFileName =  QDir::currentPath() + "temp";
                QString message = "File name cannot be blank. Using default:\n" +
                                   tempFileName;
                QMessageBox::information(this, "", message, QMessageBox::Ok);
            }
        }
    }
    else
    {
        QMessageBox::information(this, "", "No sequences loaded.", QMessageBox::Ok);
    }    
}

int AlignmentViewerWidget::getNumberOfSeqsSelected()
{
    return seqNames->getNumberOfSeqsSelected();
}

const std::vector<int>* AlignmentViewerWidget::getSelectedRows()
{
    return seqNames->getSelectedRows();
}

bool AlignmentViewerWidget::outputColumnScores()
{
    int numSeqsSelected = seqNames->getNumberOfSeqsSelected();
    if(numSeqsSelected == 0)
    {
        QMessageBox::information(this, "ClustalQt", 
                "Select sequences to be written by clicking on the names.", QMessageBox::Ok);
        return false;        
    }
    else
    {
        QString fileName;

        // Mark Jan 18th 2007: changes to remember working Dir    
        FileDialog fd;
        fileName = fd.getSaveDataFileName("qscores", this, tr("Choose a file name to save under."),
					  "Score files (*.qscores);;All Files (*)");
	    
        if(fileName == "")
        {
            //QMessageBox::information(this, "ClustalQt", "Error: Bad name format.",  //nige
	    //                         QMessageBox::Ok); //nige
            return false;        
        }
        else
        {
            // Open the file
            QFile columnScoreFile(fileName);
            if(!columnScoreFile.open(QIODevice::WriteOnly))
            {
                QMessageBox::information(this, "ClustalQt", 
                "Could not open save file.", QMessageBox::Ok);
                return false;    
            }
            else
            {    
                QTextStream out(&columnScoreFile);
                // Get the sequence numbers to be output from the names object
                const std::vector<int>* selected = seqNames->getSelectedRows();
                // Get the sequences from the alignmentToDisplay object!!
                const clustalw::SeqArray* seqArray = alignmentToDisplay->getSeqArray();
                // Get the scores from the histogram object! 
                const std::vector<int>* columnScores = histWidget->getColumnScores();
                int val;
                char res;
                bool gapCol;
                int colScore;
                int gapPos1 = clustalw::userParameters->getGapPos1();
                int gapPos2 = clustalw::userParameters->getGapPos2();
                
                // Get the maximum length of the sequences
                int maxLength = 0;
                int lengthCurrentSeq = 0;
                int numSeqs = alignmentToDisplay->getNumSeqs();
                
                for(int i = 0; i < numSeqs; i++)
                {
                    if((*selected)[i] == 1)
                    {
                        lengthCurrentSeq = alignmentToDisplay->getSeqLength(i + 1);
                        if(lengthCurrentSeq > maxLength)
                        {
                            maxLength = lengthCurrentSeq;
                        }
                    }
                }
                
                for(int j = 1; j <= maxLength; j++)
                {
                    // Check for a column of gaps!
                    gapCol = true;
                    for(int i = 1; i <= numSeqs; i++)
                    {                        
                        if((*selected)[i - 1] == 1)
                        {
                            if(j <= alignmentToDisplay->getSeqLength(i))
                            {
                                val = (*seqArray)[i][j];
                                if(val != gapPos1 && val != gapPos2)
                                {
                                    gapCol = false;
                                    break;
                                }
                            }
                        }
                    }

                    if(gapCol == false)
                    {                    
                        for(int i = 1; i <= numSeqs; i++)
                        {
                        
                            if((*selected)[i - 1] == 1)
                            {
                                if(j > alignmentToDisplay->getSeqLength(i))
                                {
                                    res = '-';
                                }
                                else
                                {
                                    val = (*seqArray)[i][j];
                                
                                    if(val == gapPos1 || val == gapPos2)
                                    {
                                        res = '-';
                                    }
                                    else
                                    {
                                        res = clustalw::userParameters->getAminoAcidCode(val);
                                    }
                                }
                                // Now write to file!
                                out << res << " "; 
                            }
                        }
                        // AW: problem: this comes from histogram data
                        // which sets max values to 99 instead of 100
                        colScore = (*columnScores)[j - 1];
                        out << "\t" << colScore <<"\n";
                    }
                }
                columnScoreFile.close();
                std::string name = fileName.toStdString();
                clustalw::utilityObject->info("File %s saved", name.c_str());
                return true;
            }
            
        }   
    }
}

int AlignmentViewerWidget::makeGapPenaltyData()
{
    int i,n = 0;    
    if(firstSeq > 0 && lastSeq >= firstSeq)
    {    
        char val;
        int len = alignmentToDisplay->getSeqLength(firstSeq);
        ssName = "";
        secStructData = "";
        bool ok = false;
        vector<char>* gapMask;
        
        if (profileNum == 1)
        {
            secStructData += QString(len, ' ');
            if (clustalw::userParameters->getStructPenalties1() == clustalw::GMASK && 
                clustalw::userParameters->getUseSS1() == true) 
            {
                gapMask = alignmentToDisplay->getGapPenaltyMask1();
                ok = true;
            }
        }
        else if (profileNum == 2)
        {
            if (clustalw::userParameters->getStructPenalties2() == clustalw::GMASK && 
                clustalw::userParameters->getUseSS2() == true) 
            {
                gapMask = alignmentToDisplay->getGapPenaltyMask2();
                ok = true;
            }
        }
        if(ok)
        {        
            n = 1;
            
            ssName = "Gap Penalties";
            
            for(i = 0; i < len && i < gapMask->size(); i++) 
            {
                val = (*gapMask)[i];
                if (val == clustalw::userParameters->getGapPos1())
                {
                    secStructData[i] = '-';
                }
                else
                {
                    secStructData[i] = val;
                }
            }
        }
    }
    return(n);
}

int AlignmentViewerWidget::makeSecStructData()
{
    int i,n = 0;
    char val;
    if(firstSeq > 0 && lastSeq >= firstSeq)
    {    
        vector<char> ssMask;
        int len = alignmentToDisplay->getSeqLength(firstSeq);
        ssName = "";
        secStructData = ""; 
        vector<char>* _secStructMask;
        bool ok = false;
        
        if (profileNum == 1)
        {
            if (clustalw::userParameters->getStructPenalties1() == clustalw::SECST 
                && clustalw::userParameters->getUseSS1() == true) 
            {
                _secStructMask = alignmentToDisplay->getSecStructMask1();
                ok = true;
            }
        }
        else if (profileNum == 2)
        {
            if (clustalw::userParameters->getStructPenalties2() == clustalw::SECST 
                && clustalw::userParameters->getUseSS2() == true) 
            {
                _secStructMask = alignmentToDisplay->getSecStructMask2();
                ok = true;
            }
        }
        
        if(ok)
        {    
            n = 1;
            ssName = "Structures";
            ssMask.assign(len + 10, 0);
            secStructData = "";
            secStructData += QString(len, ' ');
                
            for (i = 0; i < len && i < _secStructMask->size();i++)
            {
                ssMask[i] = _secStructMask->at(i);
            }
            clustalw::AlignmentOutput out;
            out.printSecStructMask(len, _secStructMask, &ssMask);

            for(i = 0; i < len && i < ssMask.size(); i++) 
            {
                val = ssMask[i];
                if (val == clustalw::userParameters->getGapPos1())
                {
                    secStructData[i] = '-';
                }
                else
                {
                    secStructData[i] = val;
                }
            }
        }
    }
    return(n);
}


void AlignmentViewerWidget::writeToPSFile(PostscriptFileParams postscriptParams)
{
    if(firstSeq != 0 && lastSeq != 0)
    {           
        PostscriptFileParams params = postscriptParams;
        clustalw::Array2D<QColor> colorMask;
        colorMask = alignWidget->getPrintoutColorMask();
    
        psPrintParams printParams;
        printParams.alignPtr = alignmentToDisplay;
        printParams.colorMask = &colorMask;
        
        if(alignWidget->getShowExceptionalResidues() && exRes != 0)
        {
            printParams.showExRes = true;
            printParams.exRes = *exRes;            
        }
        else
        {
            printParams.showExRes = false;
        }

        printParams.firstSeq = firstSeq;
        printParams.histogram = &histogram;
        printParams.lastSeq = lastSeq;
        
        if(alignWidget->getShowLowScoreSeg() && lowScoreRes != 0)
        {
            printParams.showLowScoreRes = true;
            printParams.lowScoreRes = *lowScoreRes;            
        }
        else
        {
            printParams.showLowScoreRes = false;
        }        
        
        if((profileNum == 1 && 
            clustalw::userParameters->getStructPenalties1() != clustalw::NONE && 
            clustalw::userParameters->getUseSS1() == true) ||
            (profileNum == 2 && 
            clustalw::userParameters->getStructPenalties2() != clustalw::NONE && 
            clustalw::userParameters->getUseSS2() == true))
        {
            printParams.nameHeader.resize(2);
            printParams.nameHeader[0] = "";
            printParams.nameHeader[1] = seqNames->getNameSSHeader(); 
            
            printParams.seqHeader.resize(2);
            printParams.seqHeader[0] = alignWidget->getConservationInfo();
            printParams.seqHeader[1] = alignWidget->getSecStructInfo();                       
        }
        else
        {
            printParams.nameHeader.resize(1);
            printParams.nameHeader[0] = "";
            
            printParams.seqHeader.resize(1);
            printParams.seqHeader[0] = alignWidget->getConservationInfo();
        }
        
        printParams.nSeqs = lastSeq - firstSeq + 1;;
    
        // Note need to do something with backGroundColors
        printParams.backGroundColors = alignWidget->getBackGroundColor();
        
        PSPrinter myPrinter(&printParams);
        myPrinter.writePSFile(&params);
        
        QString qtName = params.getWriteSeqsToFile();
        std::string name = qtName.toStdString();
        clustalw::utilityObject->info("Postscript file %s written", name.c_str());
    }
    else
    {
        QMessageBox::warning(this, tr("Error"), 
                tr("Error: No file loaded."));
    }
}

bool AlignmentViewerWidget::seqsLoaded()
{
    if(firstSeq != 0 && lastSeq != 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool AlignmentViewerWidget::hasFocus()
{
    int c = 0; // nige again
    c += namesScroll->hasFocus();
    c += seqNames->hasFocus();
    c += alignmentScroll->hasFocus();
    c += alignWidget->hasFocus();
    //these seem not to work:
    c += conservationScroll->hasFocus();
    c += histWidget->hasFocus();
    return (c > 0 ? true : false);
}

//nige
void AlignmentViewerWidget::recvNumSequencesChanged(int count) {
    //printf("RECV/EMIT(AVW, recvNumSequencesChanged) %0x (%d)\n", (int)this, count);
    emit numSequencesChanged(count);
}


void AlignmentViewerWidget::resizeEvent ( QResizeEvent * event )
{
    moveNamesScrollBarToRight();//Mark 20-7-07
}

/*Mark 20-7-07*/
void AlignmentViewerWidget::moveNamesScrollBarToRight()
{
    if(namesScroll != 0)
    {
        QScrollBar* horizNameScrollBar = namesScroll->horizontalScrollBar();
        int scrollBarWidth = horizNameScrollBar->maximumWidth();
        horizNameScrollBar->setValue(scrollBarWidth);
    }
}