~ubuntu-branches/ubuntu/raring/voxbo/raring

« back to all changes in this revision

Viewing changes to qtglm/permgen.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-06-06 11:33:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100606113311-v3c13imdkkd5n7ae
Tags: upstream-1.8.5~svn1172
ImportĀ upstreamĀ versionĀ 1.8.5~svn1172

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// permgen.cpp
 
3
// Copyright (c) 1998-2010 by The VoxBo Development Team
 
4
 
 
5
// This file is part of VoxBo
 
6
// 
 
7
// VoxBo is free software: you can redistribute it and/or modify it
 
8
// under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation, either version 3 of the License, or
 
10
// (at your option) any later version.
 
11
// 
 
12
// VoxBo is distributed in the hope that it will be useful, but
 
13
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
// General Public License for more details.
 
16
// 
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with VoxBo.  If not, see <http://www.gnu.org/licenses/>.
 
19
// 
 
20
// For general information on VoxBo, including the latest complete
 
21
// source code and binary distributions, manual, and associated files,
 
22
// see the VoxBo home page at: http://www.voxbo.org/
 
23
// 
 
24
// original version written by Tom King and Daniel Y. Kimberg
 
25
 
 
26
#include "permgen.h"
 
27
#include "vbx.h"
 
28
#include <iostream>
 
29
#include <fstream>
 
30
#include <qcheckbox.h>
 
31
#include <qvariant.h>
 
32
#include <qlabel.h>
 
33
#include <qpushbutton.h>
 
34
#include <q3textedit.h>
 
35
#include <qlayout.h>
 
36
#include <qtooltip.h>
 
37
#include <q3whatsthis.h>
 
38
#include <qmessagebox.h>
 
39
#include <qcombobox.h>
 
40
#include <qlineedit.h>
 
41
#include <qspinbox.h>
 
42
#include <q3frame.h>
 
43
//Added by qt3to4:
 
44
#include <Q3HBoxLayout>
 
45
#include <Q3VBoxLayout>
 
46
#include <Q3BoxLayout>
 
47
#include <QCloseEvent>
 
48
 
 
49
extern VBPrefs vbp;
 
50
 
 
51
VBSequence seq;
 
52
VBJobSpec js;
 
53
string tempPRM;
 
54
int msgboxsetpermnumber;
 
55
 
 
56
using namespace std;
 
57
 
 
58
permGenerator::permGenerator( QWidget* parent, const char* name, bool modal, Qt::WFlags fl )
 
59
  : QDialog( parent, name, modal, fl )
 
60
 
 
61
{
 
62
  if ( !name )
 
63
    setName( "permGenerator" );
 
64
 
 
65
  Q3BoxLayout *x = new Q3VBoxLayout(this); 
 
66
 
 
67
  txtPRM = new QLineEdit( this, "txtPRM" );
 
68
  txtPRM->setGeometry( QRect( 250, 10, 170, 24 ) );
 
69
  connect(txtPRM, SIGNAL(textChanged(const QString &)), this, SLOT(CalculateNumPerms()));        
 
70
 
 
71
  pbDir = new QPushButton( this, "pbDir" );
 
72
  pbDir->setGeometry( QRect( 50, 10, 180, 20 ) );
 
73
  connect(pbDir, SIGNAL(clicked()), this, SLOT(getPRMFile()));
 
74
 
 
75
  Q3BoxLayout *y = new Q3HBoxLayout(x);
 
76
  y->insertWidget(0, pbDir, 10, Qt::AlignRight);
 
77
  y->addSpacing(30); 
 
78
  y->insertWidget(2, txtPRM, 10, Qt::AlignTop);
 
79
  y->addStretch(1);               
 
80
  y->setMargin(5);  
 
81
                                                                                                          
 
82
  txtPermDir = new QLineEdit( this, "txtPermDir" );
 
83
  txtPermDir->setGeometry( QRect( 250, 40, 170, 24 ) );
 
84
 
 
85
  lblPermDir = new QLabel( this, "lblPermNumber" );
 
86
  lblPermDir->setGeometry( QRect( 90, 130, 140, 20 ) );
 
87
 
 
88
  Q3BoxLayout *y1 = new Q3HBoxLayout(x);
 
89
  y1->insertWidget(0, lblPermDir, 10, Qt::AlignRight);
 
90
  y1->addSpacing(30);
 
91
  y1->insertWidget(2, txtPermDir, 10, Qt::AlignTop);
 
92
  y1->addStretch(1);
 
93
  y1->setMargin(5);
 
94
                                                                                                             
 
95
  txtContrasts = new QLineEdit( this, "txtContrasts" );
 
96
  txtContrasts->setGeometry( QRect( 250, 70, 170, 24 ) );
 
97
 
 
98
  lblContrasts = new QLabel( this, "lblContrasts" );
 
99
  lblContrasts->setGeometry( QRect( 130, 70, 100, 20 ) );
 
100
 
 
101
  Q3BoxLayout *y2 = new Q3HBoxLayout(x);
 
102
  y2->insertWidget(0, lblContrasts, 10, Qt::AlignRight);
 
103
  y2->addSpacing(30);
 
104
  y2->insertWidget(2, txtContrasts, 10, Qt::AlignTop);
 
105
  y2->addStretch(1);
 
106
  y2->setMargin(5);
 
107
 
 
108
  cbScale = new QComboBox( this, "txtScale" );
 
109
  cbScale->setGeometry( QRect( 250, 100, 170, 24 ) );
 
110
                                                                                                               
 
111
  lblScale = new QLabel( this, "lblScale" );
 
112
  lblScale->setGeometry( QRect( 130, 100, 100, 20 ) );
 
113
                                                                                                              
 
114
  Q3BoxLayout *y5 = new Q3HBoxLayout(x);
 
115
  y5->insertWidget(0, lblScale, 10, Qt::AlignRight);
 
116
  y5->addSpacing(30);
 
117
  y5->insertWidget(2, cbScale, 10, Qt::AlignTop);
 
118
  y5->addStretch(1);
 
119
  y5->setMargin(5);
 
120
                                                                                                               
 
121
  txtPermNumber = new QLineEdit( this, "txtPermNumber" );
 
122
  txtPermNumber->setGeometry( QRect( 250, 130, 170, 24 ) );
 
123
  connect(txtPermNumber, SIGNAL(textChanged(const QString &)), this, SLOT(warnCalculateNumPerms()));            
 
124
 
 
125
  lblPermNumber = new QLabel( this, "lblPermNumber" );
 
126
  lblPermNumber->setGeometry( QRect( 90, 130, 140, 20 ) );
 
127
 
 
128
  Q3BoxLayout *y3 = new Q3HBoxLayout(x);
 
129
  y3->insertWidget(0, lblPermNumber, 10, Qt::AlignRight);
 
130
  y3->addSpacing(30);
 
131
  y3->insertWidget(2, txtPermNumber, 10, Qt::AlignTop);
 
132
  y3->addStretch(1); 
 
133
  y3->setMargin(5);            
 
134
 
 
135
  lblPseudoT = new QLabel( this, "lblPseudoT" );
 
136
  lblPseudoT->setGeometry( QRect(10, 150, 143, 40) );
 
137
                                                                                                               
 
138
  QCheckBox *on=new QCheckBox("on/off",this);
 
139
  on->setChecked(0);
 
140
  on->setGeometry( QRect( 160, 160, 60, 21 ) );
 
141
  QObject::connect(on,SIGNAL(toggled(bool)),this,SLOT(mod(bool)));            
 
142
                              
 
143
  txtZ = new QLineEdit( this, "txtZ" );
 
144
  txtZ->setGeometry( QRect( 390, 160, 30, 24 ) );
 
145
  txtZ->setDisabled(true);
 
146
                                                                                                              
 
147
  lblZ = new QLabel( this, "lblZ" );
 
148
  lblZ->setGeometry( QRect( 370, 160, 16, 21 ) );
 
149
                                                                                                              
 
150
  txtX = new QLineEdit( this, "txtX" );
 
151
  txtX->setGeometry( QRect( 270, 160, 30, 24 ) );
 
152
  txtX->setDisabled(true);
 
153
                                                                                                              
 
154
  txtY = new QLineEdit( this, "txtY" );
 
155
  txtY->setGeometry( QRect( 330, 160, 30, 24 ) );
 
156
  txtY->setDisabled(true);
 
157
                                                                                                                
 
158
  lblX = new QLabel( this, "lblX" );
 
159
  lblX->setGeometry( QRect( 250, 160, 16, 20 ) );
 
160
                                                                                                                
 
161
  lblY = new QLabel( this, "lblY" );
 
162
  lblY->setGeometry( QRect( 310, 160, 16, 20 ) );
 
163
 
 
164
  Q3BoxLayout *y4 = new Q3HBoxLayout(x);
 
165
  y4->addSpacing(10);
 
166
  y4->insertWidget(1, lblPseudoT, 10, Qt::AlignRight);
 
167
  y4->addSpacing(10);
 
168
  y4->insertWidget(3, on, 10, Qt::AlignRight);
 
169
  y4->addSpacing(10);
 
170
  y4->insertWidget(5, lblX, 10, Qt::AlignRight);
 
171
  y4->addSpacing(5);
 
172
  y4->insertWidget(7, txtX, 10, Qt::AlignTop);
 
173
  y4->addSpacing(10);
 
174
  y4->insertWidget(9, lblY, 10, Qt::AlignRight);
 
175
  y4->addSpacing(5);
 
176
  y4->insertWidget(11, txtY, 10, Qt::AlignTop);
 
177
  y4->addSpacing(10);
 
178
  y4->insertWidget(13, lblZ, 10, Qt::AlignRight);
 
179
  y4->addSpacing(5);
 
180
  y4->insertWidget(15, txtZ, 10, Qt::AlignTop);
 
181
  y4->addStretch(1);
 
182
  y4->setMargin(5);
 
183
 
 
184
  lblPermType = new QLabel( this, "lblPermType" );
 
185
  lblPermType->setGeometry( QRect( 0, 190, 230, 20 ) );
 
186
                                                                                                                       
 
187
  cbPermType = new QComboBox( this, "cbPermType" );
 
188
  cbPermType->setGeometry( QRect( 250, 190, 170, 24 ) );
 
189
                                                                                                              
 
190
  Q3BoxLayout *y6 = new Q3HBoxLayout(x);
 
191
  y6->insertWidget(0, lblPermType, 10, Qt::AlignRight);
 
192
  y6->addSpacing(30);
 
193
  y6->insertWidget(2, cbPermType, 10, Qt::AlignTop);
 
194
  y6->addStretch(1);
 
195
  y6->setMargin(5);
 
196
 
 
197
  line2 = new Q3Frame( this, "line2" );
 
198
  line2->setGeometry( QRect( 21, 220, 400, 20 ) );
 
199
  line2->setFrameShape( Q3Frame::HLine );
 
200
  line2->setFrameShadow( Q3Frame::Sunken );
 
201
  line2->setFrameShape( Q3Frame::HLine );
 
202
                                                                                                              
 
203
  Q3BoxLayout *y7 = new Q3HBoxLayout(x);
 
204
  y7->addSpacing(10);
 
205
  y7->addWidget(line2, 10, Qt::AlignTop);
 
206
  y7->addSpacing(10);
 
207
  y7->setMargin(5);
 
208
 
 
209
  sbPriority = new QSpinBox( this, "sbPriority" );
 
210
  sbPriority->setGeometry( QRect( 360, 240, 41, 20 ) );
 
211
  sbPriority->setMaxValue( 4 );
 
212
  sbPriority->setMinValue( 1 );
 
213
  sbPriority->setValue( 3 );
 
214
                                                                                                              
 
215
  txtSN = new QLineEdit( this, "txtSN" );
 
216
  txtSN->setGeometry( QRect( 140, 240, 130, 23 ) );
 
217
                                                                                                               
 
218
  lblPriority = new QLabel( this, "lblPriority" );
 
219
  lblPriority->setGeometry( QRect( 300, 240, 50, 21 ) );
 
220
                                                                                                               
 
221
  lblSN = new QLabel( this, "lblSequenceName" );
 
222
  lblSN->setGeometry( QRect( 30, 240, 111, 21 ) );
 
223
                                                                                                                
 
224
  Q3BoxLayout *y8 = new Q3HBoxLayout(x);
 
225
  y8->addSpacing(30);
 
226
  y8->insertWidget(1, lblSN, 10, Qt::AlignRight);
 
227
  y8->addSpacing(30);
 
228
  y8->insertWidget(3, txtSN, 10, Qt::AlignTop);
 
229
  y8->addSpacing(10);
 
230
  y8->insertWidget(5, lblPriority, 10, Qt::AlignRight);
 
231
  y8->addSpacing(30);
 
232
  y8->insertWidget(7, sbPriority, 10, Qt::AlignLeft);
 
233
  y8->addStretch(1);
 
234
  y8->setMargin(5);
 
235
 
 
236
  //QBoxLayout *y9 = new QHBoxLayout(x);
 
237
  //y9->addSpacing(30);
 
238
  //y9->addStretch(1);
 
239
 
 
240
  pbCancel = new QPushButton( this, "pbCancel" );
 
241
  pbCancel->setGeometry( QRect( 310, 280, 110, 30 ) );
 
242
  connect(pbCancel, SIGNAL(clicked()), this, SLOT(exitProgram()));
 
243
 
 
244
  pbGO = new QPushButton( this, "pbGO" );
 
245
  pbGO->setGeometry( QRect( 30, 280, 120, 30 ) );
 
246
  connect(pbGO, SIGNAL(clicked()), this, SLOT(pressed()));
 
247
 
 
248
  Q3BoxLayout *y10 = new Q3HBoxLayout(x);
 
249
  y10->addSpacing(30);
 
250
  y10->insertWidget(1, pbGO, 10, Qt::AlignTop);
 
251
  y10->addSpacing(100); 
 
252
  y10->insertWidget(3, pbCancel, 10, Qt::AlignTop);
 
253
  y10->addSpacing(30);
 
254
  y10->addStretch(1);
 
255
  y10->setMargin(5);
 
256
 
 
257
  languageChange();
 
258
  resize( QSize(436, 318).expandedTo(minimumSizeHint()) );
 
259
}
 
260
 
 
261
/*
 
262
 *  Destroys the object and frees any allocated resources
 
263
 */
 
264
permGenerator::~permGenerator()
 
265
{
 
266
  // no need to delete child widgets, Qt does it all for us
 
267
}
 
268
 
 
269
/*
 
270
 *  Sets the strings of the subwidgets using the current
 
271
 *  language.
 
272
 */
 
273
void permGenerator::languageChange()
 
274
{
 
275
  setCaption( tr( "Permutation Generator" ) );
 
276
  lblZ->setText( tr( "z:" ) );
 
277
  lblX->setText( tr( "x:" ) );
 
278
  lblY->setText( tr( "y:" ) );
 
279
  txtX->insert("0");
 
280
  txtY->insert("0");
 
281
  txtZ->insert("0");
 
282
  pbGO->setText( tr( "Submit Jobs" ) );
 
283
  lblContrasts->setText( tr( "Contrast:" ) );
 
284
  lblScale->setText( tr( "Scale:" ) );
 
285
  txtPermNumber->insert("0");
 
286
  lblPermNumber->setText( tr( "Number of Permutations:" ) );
 
287
  lblPseudoT->setText( tr( "Smoothing for Pseudo-t \n"
 
288
                           "map (FWHM in Voxels):" ) );
 
289
  lblPermType->setText( tr( " Perm Type:" ) );
 
290
  pbDir->setText( tr( "Select PRM File" ) );
 
291
  lblPriority->setText( tr( "Priority:" ) );
 
292
  lblSN->setText( tr( "Sequence Name:" ) );
 
293
  lblPermDir->setText( tr( "Perm Directory (to be created):" ) );
 
294
 
 
295
  cbScale->addItem("t values");
 
296
  cbScale->addItem("intercept % change");
 
297
  cbScale->addItem("raw beta values");
 
298
  cbScale->addItem("F values");
 
299
  cbScale->addItem("p map for t values");
 
300
  cbScale->addItem("p map for F values");
 
301
  cbScale->addItem("Z map for t values");
 
302
  cbScale->addItem("Z map for F values");
 
303
 
 
304
  cbPermType->addItem("Sign");
 
305
  cbPermType->addItem("Regular");
 
306
 
 
307
  pbCancel->setText( tr( "Cancel" ) );
 
308
  msgboxsetpermnumber = 0;
 
309
}
 
310
 
 
311
void permGenerator::mod(bool x) {
 
312
  txtX->setEnabled(x);
 
313
  txtY->setEnabled(x);
 
314
  txtZ->setEnabled(x);
 
315
  if (x == FALSE) {
 
316
    txtX->clear();
 
317
    txtX->insert("0");
 
318
    txtY->clear();
 
319
    txtY->insert("0");
 
320
    txtZ->clear();
 
321
    txtZ->insert("0");
 
322
  }
 
323
  return;
 
324
}
 
325
QString permGenerator::getPRMFile() {
 
326
  QString s=Q3FileDialog::getOpenFileName(".","All (*.prm)",this,"open .prm file","Choose a .prm file to load");
 
327
  if (s==QString::null)
 
328
    return QString("");
 
329
  tempPRM = s.ascii(); 
 
330
  txtPRM->clear();
 
331
  createfullpath(s.ascii());
 
332
  txtPRM->insert(s.ascii());
 
333
  if (s==QString::null)
 
334
    return QString("");
 
335
  else
 
336
    return s;
 
337
}
 
338
 
 
339
int commandlineExecute(int argc, char **argv) {
 
340
  GLMInfo glmi;
 
341
  string matrixStemName, permDir, sscale, seqname;
 
342
  int method, priority;
 
343
  string scale;
 
344
  string contrasts, pseudoT, clist, plist;
 
345
  arghandler a;
 
346
  a.setArgs("-h", "--help", 0);
 
347
  a.setArgs("-m", "--matrixstemname", 1);
 
348
  a.setArgs("-d", "--permdir", 1);
 
349
  a.setArgs("-t", "--method", 1);
 
350
  a.setArgs("-v", "--version", 0);
 
351
  a.setArgs("-n", "--permindex", 1);
 
352
  a.setArgs("-s", "--scale", 1);
 
353
  a.setArgs("-c", "--contrast", 1);
 
354
  a.setArgs("-p", "--pseudot", 3);
 
355
  a.setArgs("-a", "--seqname", 1);
 
356
  a.setArgs("-b", "--priority", 1);
 
357
  a.parseArgs(argc, argv);
 
358
  string errstring = a.badArg();
 
359
  if (errstring.size()) 
 
360
    return 1;
 
361
  matrixStemName = a.getFlaggedArgs("-m")[0];
 
362
  if (matrixStemName.size() == 0) return 2;
 
363
  glmi.setup(matrixStemName);
 
364
  permDir = a.getFlaggedArgs("-d")[0];
 
365
  if (permDir.size() == 0) return 3;
 
366
  method = atoi(a.getFlaggedArgs("-t")[0].c_str());
 
367
  if (a.getFlaggedArgs("-t")[0].size() == 0) return 4;
 
368
  if (a.flagPresent("-v"))
 
369
    printf("\nVoxBo v%s\n",vbversion.c_str());
 
370
  int numPerms = atoi(a.getFlaggedArgs("-n")[0].c_str());
 
371
  int maxPerms = 0;
 
372
  int onOff = 0;
 
373
  VBMatrix headerMatrix;
 
374
  string headerName = xrootname(glmi.stemname) + ".G";
 
375
  headerMatrix.ReadMAT1Header(headerName);
 
376
  double orderG = (double)headerMatrix.m;
 
377
  if (pow(2.0, (orderG-1)) < 1000)
 
378
    maxPerms = (int)pow(2.0, (orderG-1));
 
379
  else
 
380
    maxPerms = 1000;
 
381
  if (numPerms > maxPerms) return 8;
 
382
  if (a.getFlaggedArgs("-n")[0].size() == 0) return 5;
 
383
  sscale = a.getFlaggedArgs("-s")[0];
 
384
  if (sscale.size() == 0) return 6;
 
385
  seqname = a.getFlaggedArgs("-a")[0];
 
386
  if (seqname.size() == 0) seqname = glmi.stemname;
 
387
  priority = atoi(a.getFlaggedArgs("-b")[0].c_str());
 
388
  if (a.getFlaggedArgs("-b")[0].size() == 0) priority = 3;
 
389
  clist =a.getFlaggedArgs("-c")[0];
 
390
  if (a.getFlaggedArgs("-c")[0].size() == 0) return 7;
 
391
  plist = a.getFlaggedArgs("-p")[0] + " " + a.getFlaggedArgs("-p")[1] + " " + a.getFlaggedArgs("-p")[2];
 
392
  if (a.getFlaggedArgs("-p")[0].size() == 0) 
 
393
    plist = "'0 0 0'";
 
394
  else
 
395
    onOff = 1;
 
396
  char presentDir[STRINGLEN];
 
397
  char temp[STRINGLEN];
 
398
  char tempstring[STRINGLEN];
 
399
  getcwd(presentDir,STRINGLEN-1);
 
400
  strcat(presentDir, "/");
 
401
  struct stat my_stat;
 
402
  string file;
 
403
  //check permutation directory if mat file already exists
 
404
  file = glmi.stemname + "_" + permDir + "/" + "permutations.mat";
 
405
  //clist = "'" + clist + "'";
 
406
  if (clist.size()) {
 
407
    if (glmi.parsecontrast(clist) != 0) {
 
408
      errstring = "contrast: failed to derive a valid contrast from " + clist;
 
409
      printErrorMsg(VB_ERROR, errstring.c_str()); 
 
410
      return -1;
 
411
    }
 
412
    else {
 
413
      contrasts = "";
 
414
      for (int index = 0; index < glmi.contrast.contrast.size(); index++) {
 
415
        sprintf(tempstring, "%1.0f", glmi.contrast.contrast[index]);
 
416
        if (index != 0) contrasts += " ";
 
417
        contrasts += tempstring;
 
418
      }
 
419
    }
 
420
  }
 
421
  else {
 
422
    errstring = "contrast: no contrast values were supplied.";
 
423
    printErrorMsg(VB_ERROR, errstring.c_str()); 
 
424
    return -1; 
 
425
  }
 
426
  clist = "'";
 
427
  for (int index = 0; index < glmi.contrast.contrast.size(); index++) {
 
428
    sprintf(tempstring, "%1.0f", glmi.contrast.contrast[index]);
 
429
    if (index != 0) clist += " ";
 
430
    clist += tempstring;
 
431
  }
 
432
  clist += "'";
 
433
  if (!sscale.size()) {
 
434
    if (glmi.contrast.scale.size())
 
435
      sscale = glmi.contrast.scale;
 
436
    else {
 
437
      errstring = "vbpermgen: no scale was provided.";
 
438
      printErrorMsg(VB_ERROR, errstring.c_str());
 
439
      return -1;
 
440
    }
 
441
  }
 
442
  if (!(validscale(sscale)) && sscale.size()) {
 
443
    errstring = "contrast: invalid scale passed: " + sscale;
 
444
    printErrorMsg(VB_ERROR, errstring.c_str());
 
445
    return -1;
 
446
  }
 
447
  plist = "'" + plist + "'";
 
448
  permclass pc;
 
449
  pc.AddMatrixStemName(glmi.stemname);
 
450
  pc.AddPermDir(permDir);
 
451
  pc.AddScale(sscale);
 
452
  pc.AddMethod(method);
 
453
  pc.AddContrastlist(contrasts);
 
454
  pc.AddPseudoTList(plist);
 
455
  pc.AddPseudoTOnOff(onOff);
 
456
  pc.SetFileName("perminfo.txt");
 
457
  pc.AddPrmTimeStamp(glmi.stemname);
 
458
  if (stat(file.c_str(), &my_stat) != 0)
 
459
    permStart(pc);
 
460
  pc.SavePermClass();
 
461
  memset((void*)&my_stat, 0, sizeof(my_stat));
 
462
  seq.init();
 
463
  QString name;
 
464
  int count = 0;
 
465
  if (numPerms > 0) {
 
466
    for (int permIndex = 0; permIndex < numPerms; permIndex++) {
 
467
      js.init();
 
468
      js.arguments.clear();
 
469
      if (method == 1)
 
470
        js.jobtype="permstep";
 
471
      else
 
472
        js.jobtype="permstep2";
 
473
 
 
474
      // Modified by Dongbo to add the argument title ina dumb way (10/12/2005)
 
475
      //          js.arguments.Add(glmi.stemname);
 
476
      //          js.arguments.Add(permDir);
 
477
      //          sprintf(temp, "%d", method); 
 
478
      //          js.arguments.Add(temp); 
 
479
      //          sprintf(temp, "%s", scale.c_str());
 
480
      //          js.arguments.Add(temp);
 
481
      //          js.arguments.Add(clist);
 
482
      //          js.arguments.Add(plist);
 
483
      //          sprintf(temp, "%d", permIndex);
 
484
      //          js.arguments.Add(temp);
 
485
      js.arguments["matrixstemname"]=glmi.stemname;
 
486
      js.arguments["permdir"]=permDir;
 
487
      js.arguments["permtype"]=strnum(method);
 
488
      // FIXME steps is set to sscale???
 
489
      js.arguments["steps"]=temp;
 
490
      js.arguments["contrast"]=clist;
 
491
      js.arguments["pseudot"]=plist;
 
492
      js.arguments["permindex"]=strnum(permIndex);
 
493
 
 
494
      js.magnitude=0;
 
495
      js.name=permDir+temp;
 
496
      js.dirname = presentDir;
 
497
      js.jnum = permIndex;
 
498
      count = permIndex;
 
499
      seq.addJob(js);
 
500
    }
 
501
  }
 
502
  js.init();
 
503
  js.arguments.clear();
 
504
  js.jobtype="permfinish";
 
505
 
 
506
  // Modified by Dongbo too (10/12/2005)
 
507
  //    js.arguments.Add(glmi.stemname + "_" + permDir + "/" + "iterations/permcube");
 
508
  //    js.arguments.Add(glmi.stemname + "_" + permDir + "/results.ref");
 
509
  js.arguments["permstemname"]=glmi.stemname + "_" + permDir + "/" + "iterations/permcube";
 
510
  js.arguments["resultsfile"]=glmi.stemname + "_" + permDir + "/results.ref";
 
511
 
 
512
  js.magnitude=0;
 
513
  js.name="finish";
 
514
  js.dirname = presentDir;
 
515
  js.jnum = count + 1;
 
516
  for (int i=0; i<=count; i++)
 
517
    js.waitfor.insert(i);
 
518
  seq.addJob(js);
 
519
  seq.name=seqname;
 
520
  if (seq.name.size() == 0)
 
521
    seq.name="perm"+matrixStemName;
 
522
  seq.priority = priority;
 
523
  seq.seqnum = (int)getpid();
 
524
  if (vbp.cores==0) {
 
525
    if (seq.Submit(vbp) == 0) {
 
526
      errstring = "vbpermgen successfully submitted the sequence.";
 
527
      printErrorMsg(VB_INFO, errstring.c_str());
 
528
    }
 
529
    else {
 
530
      errstring = "vbpermgen failed to submit the sequence.";
 
531
      printErrorMsg(VB_ERROR, errstring.c_str());
 
532
      return -1;
 
533
    }
 
534
  }
 
535
  else {
 
536
    runseq(vbp,seq,vbp.cores);
 
537
  }
 
538
  return 0;
 
539
}
 
540
 
 
541
void permGenerator::pressed() {
 
542
  GLMInfo glmi;
 
543
  char presentDir[STRINGLEN];
 
544
  char temp[STRINGLEN];
 
545
  char tempstring[STRINGLEN];
 
546
  getcwd(presentDir,STRINGLEN-1);
 
547
  strcat(presentDir, "/");
 
548
  string matrixStemName = txtPRM->text().latin1();
 
549
  string permDir = txtPermDir->text().latin1();
 
550
  string scale = cbScale->currentText().latin1();
 
551
  int method = 2-(cbPermType->currentItem()); //methods start at 1, not 0
 
552
  int numPerms = atoi(txtPermNumber->text());
 
553
  string contrasts = txtContrasts->text().latin1();
 
554
  string clist = contrasts;
 
555
  if (clist.size()) {
 
556
    glmi.setup(matrixStemName);
 
557
    if (glmi.parsecontrast(clist) != 0) {
 
558
      string errstring = "contrast: failed to derive a valid contrast from " + clist + "\n";
 
559
      QMessageBox::warning( this, "Warning!", errstring.c_str());
 
560
      return;
 
561
    }
 
562
    else {
 
563
      contrasts = "";
 
564
      for (int index = 0; index < glmi.contrast.contrast.size(); index++) {
 
565
        sprintf(tempstring, "%1.0f", glmi.contrast.contrast[index]);
 
566
        if (index != 0) contrasts += " ";
 
567
        contrasts += tempstring;
 
568
      }
 
569
    }
 
570
  }
 
571
  else {
 
572
    string errstring = "contrast: no contrast values were supplied.\n";
 
573
    QMessageBox::warning( this, "Warning!", errstring.c_str());
 
574
    return;
 
575
  }
 
576
  clist = "'";
 
577
  for (int index = 0; index < glmi.contrast.contrast.size(); index++) {
 
578
    sprintf(tempstring, "%1.0f", glmi.contrast.contrast[index]);
 
579
    if (index != 0) clist += " ";
 
580
    clist += tempstring;
 
581
  }
 
582
  clist += "'";
 
583
  if (!scale.size()) {
 
584
    if (glmi.contrast.scale.size())
 
585
      scale = glmi.contrast.scale;
 
586
    else {
 
587
      string errstring = "vbpermgen: no scale was provided.";
 
588
      printErrorMsg(VB_ERROR, errstring.c_str());
 
589
      return;
 
590
    }
 
591
  }
 
592
  if (scale == "t values") scale = "t";
 
593
  else if (scale == "intercept % change") scale = "i";
 
594
  else if (scale == "raw beta values") scale = "rb";
 
595
  else if (scale == "F values") scale = "f";
 
596
  else if (scale == "p map for t values") scale = "tp";
 
597
  else if (scale == "p map for F values") scale = "fp";
 
598
  else if (scale == "Z map for t values") scale = "tz";
 
599
  else if (scale == "Z map for F values") scale = "fz";
 
600
  if (!(validscale(scale)) && scale.size()) {
 
601
    string errstring = "contrast: invalid scale passed: " + scale;
 
602
    QMessageBox::warning( this, "Warning!", errstring.c_str()); 
 
603
    return;
 
604
  }
 
605
  string pseudoT = (string)txtX->text().latin1() + " " + txtY->text().latin1() + " " + txtZ->text().latin1();
 
606
  if (!txtX->isEnabled())
 
607
    pseudoT = "0 0 0";
 
608
  string plist = "'" + pseudoT + "'"; 
 
609
  permclass pc;
 
610
  pc.AddMatrixStemName(glmi.stemname);
 
611
  pc.AddPermDir(permDir);
 
612
  pc.AddScale(scale);
 
613
  pc.AddMethod(method);
 
614
  pc.AddContrastlist(contrasts);
 
615
  pc.AddPseudoTList(pseudoT);
 
616
  pc.AddPseudoTOnOff(txtX->isEnabled());
 
617
  pc.AddPrmTimeStamp(glmi.stemname);
 
618
  pc.SetFileName("perminfo.txt");
 
619
  if (matrixStemName.size() == 0) {
 
620
    QMessageBox::warning( this, "Warning!", "Must specify the matrix stem name (glm directory).\n");
 
621
    return;
 
622
  }
 
623
  if (permDir.size() == 0) {
 
624
    QMessageBox::warning( this, "Warning!", "Must specify the permutation directory name.\n"); 
 
625
    return;
 
626
  }
 
627
  if ((method < 0) || (method > 2)) {
 
628
    QMessageBox::warning( this, "Warning!", "Must specify the permutation method.\n"); 
 
629
    return;
 
630
  }
 
631
  if ((scale != "t") && (scale != "i") && (scale != "rb") && (scale != "f") && (scale != "tp") &&
 
632
      (scale != "fp") && (scale != "tz") && (scale != "fz")) { 
 
633
    QMessageBox::warning( this, "Warning!", "Must specify the scale. For list of scales, type permgen -h.\n");
 
634
    return;
 
635
  }
 
636
  struct stat my_stat;
 
637
  string file;
 
638
  //check permutation directory if mat file already exists
 
639
  file = glmi.stemname + "_" + permDir + "/" + "permutations.mat"; 
 
640
  // FIXME if permstart returns an error, we're in trouble.  but
 
641
  // really this should be a job instead.
 
642
  if (stat(file.c_str(), &my_stat) != 0) 
 
643
    permStart(pc);
 
644
  pc.SavePermClass(); 
 
645
  memset((void*)&my_stat, 0, sizeof(my_stat));
 
646
  seq.init();
 
647
  QString name;
 
648
  int count = 0;
 
649
  if (numPerms > 0) {
 
650
    for (int permIndex = 0; permIndex < numPerms; permIndex++) {
 
651
      js.init();
 
652
      js.arguments.clear();
 
653
      if (method == 1)
 
654
        js.jobtype="permstep";
 
655
      else
 
656
        js.jobtype="permstep2";
 
657
 
 
658
      // Modified by Dongbo to add the argument title ina dumb way (10/12/2005)
 
659
      //          js.arguments.Add(glmi.stemname);
 
660
      //          js.arguments.Add(permDir);
 
661
      //          sprintf(temp, "%d", method); 
 
662
      //          js.arguments.Add(temp); 
 
663
      //          sprintf(temp, "%s", scale.c_str());
 
664
      //          js.arguments.Add(temp);
 
665
      //          js.arguments.Add(clist);
 
666
      //          js.arguments.Add(plist);
 
667
      //          sprintf(temp, "%d", permIndex);
 
668
      //          js.arguments.Add(temp);
 
669
      js.arguments["matrixstemname"]=glmi.stemname;
 
670
      js.arguments["permdir"]=permDir;
 
671
      js.arguments["permtype"]=strnum(method);
 
672
      // FIXME steps = scale???
 
673
      js.arguments["steps"]=scale;
 
674
      js.arguments["contrast"]=clist;
 
675
      js.arguments["pseudot"]=plist;
 
676
      js.arguments["permindex"]=strnum(permIndex);
 
677
 
 
678
      // FIXME following flag line is totally broken
 
679
      // if (method == 2) js.arguments.Add("-e");
 
680
      js.magnitude=0;
 
681
      js.name=permDir+temp;
 
682
      js.dirname = presentDir;
 
683
      js.jnum = permIndex;
 
684
      count = permIndex;
 
685
      seq.addJob(js);
 
686
    }
 
687
  }
 
688
  js.init();
 
689
  js.arguments.clear();
 
690
  js.jobtype="permfinish";
 
691
 
 
692
  // Modified by Dongbo too (10/12/2005)
 
693
  //    js.arguments.Add(glmi.stemname + "_" + permDir + "/" + "iterations/permcube");
 
694
  //    js.arguments.Add(glmi.stemname + "_" + permDir + "/results.ref");
 
695
  js.arguments["permstemname"]=glmi.stemname + "_" + permDir + "/" + "iterations/permcube";
 
696
  js.arguments["resultsfile"]=glmi.stemname + "_" + permDir + "/results.ref";
 
697
 
 
698
  js.magnitude=0;
 
699
  js.name="finish";
 
700
  js.dirname = presentDir;
 
701
  js.jnum = count + 1;
 
702
  for (int i=0; i<=count; i++)
 
703
    js.waitfor.insert(i);
 
704
  seq.addJob(js);
 
705
  seq.name=txtSN->text().ascii();
 
706
  if (seq.name.size() == 0)
 
707
    seq.name="perm"+matrixStemName;
 
708
  seq.priority = sbPriority->value();
 
709
  seq.seqnum = (int)getpid();
 
710
  if (vbp.cores==0) {
 
711
    if (seq.Submit(vbp) == 0) {
 
712
      QMessageBox::warning( this, "Information", "The requested sequence was submitted.\n");
 
713
      return;
 
714
    }
 
715
    else {
 
716
      QMessageBox::warning( this, "Information", "The requested sequence failed to submit.\n");
 
717
      return;
 
718
    }
 
719
  }
 
720
  else {
 
721
    runseq(vbp,seq,vbp.cores);
 
722
  }
 
723
  msgboxsetpermnumber = 0;
 
724
  return;
 
725
}
 
726
 
 
727
int permGenerator::warnCalculateNumPerms() {
 
728
  if (msgboxsetpermnumber) return 1;
 
729
  VBMatrix headerMatrix;
 
730
  GLMInfo glmi;
 
731
  struct stat my_stat;
 
732
  int numPerms = 0;
 
733
  string matrixStemName;
 
734
  if (txtPRM->text().length())
 
735
    matrixStemName = txtPRM->text().ascii();
 
736
  else
 
737
    matrixStemName = tempPRM; 
 
738
  string headerName = xrootname(matrixStemName) + ".G";
 
739
  if (stat(headerName.c_str(),&my_stat)) {
 
740
    return 1;
 
741
  }
 
742
  headerMatrix.ReadMAT1Header(headerName);
 
743
  double orderG = (double)headerMatrix.m;
 
744
  if (pow(2.0, (orderG-1)) < 1000)
 
745
    numPerms = (int)pow(2.0, (orderG-1));
 
746
  else
 
747
    numPerms = 1000;
 
748
  char num[STRINGLEN];
 
749
  char obs[STRINGLEN];
 
750
  char old[STRINGLEN];
 
751
  sprintf(num, "%d", numPerms);
 
752
  sprintf(obs, "%d", (int)orderG);
 
753
  sprintf(old, "%d", atoi(txtPermNumber->text().ascii()));
 
754
  string snum = num;
 
755
  string og = obs;
 
756
  string sold = old;
 
757
  string msg = "Maximum permutations is " + snum + " based on your data.\n Do you want to generate the " + snum + " possible?";
 
758
  if (atoi(txtPermNumber->text().ascii()) > numPerms) {
 
759
    QMessageBox mb( "vbpermgen",
 
760
                    msg.c_str(),
 
761
                    QMessageBox::Information,
 
762
                    QMessageBox::Yes | QMessageBox::Default,
 
763
                    QMessageBox::No,
 
764
                    QMessageBox::Cancel | QMessageBox::Escape );
 
765
    mb.setButtonText( QMessageBox::Yes, "Replace" );
 
766
    mb.setButtonText( QMessageBox::No, "Abort" );
 
767
    switch( mb.exec() ) {
 
768
    case QMessageBox::Yes:
 
769
      msgboxsetpermnumber = 1;
 
770
      txtPermNumber->clear();
 
771
      txtPermNumber->insert(num); 
 
772
      msgboxsetpermnumber = 0;
 
773
      return 0;
 
774
      break;
 
775
    case QMessageBox::No:
 
776
      msgboxsetpermnumber = 1;
 
777
      txtPermNumber->clear();
 
778
      txtPermNumber->insert("0");
 
779
      msgboxsetpermnumber = 0;
 
780
      return 1;
 
781
      break;
 
782
    case QMessageBox::Cancel:
 
783
      msgboxsetpermnumber = 1;
 
784
      txtPermNumber->clear();
 
785
      txtPermNumber->insert("0");
 
786
      msgboxsetpermnumber = 0; 
 
787
      return 1; 
 
788
      break;
 
789
    } 
 
790
  }
 
791
  return numPerms;
 
792
}
 
793
 
 
794
void permGenerator::CalculateNumPerms() {
 
795
  if (msgboxsetpermnumber) return;
 
796
  VBMatrix headerMatrix;
 
797
  GLMInfo glmi;
 
798
  struct stat my_stat;
 
799
  int numPerms = 0;
 
800
  string matrixStemName;
 
801
  if (txtPRM->text().length())
 
802
    matrixStemName = txtPRM->text().ascii();
 
803
  else
 
804
    matrixStemName = tempPRM;
 
805
  string headerName = xrootname(matrixStemName) + ".G";
 
806
  if (stat(headerName.c_str(),&my_stat)) {
 
807
    return;
 
808
  }
 
809
  headerMatrix.ReadMAT1Header(headerName);
 
810
  double orderG = (double)headerMatrix.m;
 
811
  if (pow(2.0, (orderG-1)) < 1000)
 
812
    numPerms = (int)pow(2.0, (orderG-1));
 
813
  else
 
814
    numPerms = 1000;
 
815
  char num[STRINGLEN];
 
816
  sprintf(num, "%d", numPerms);
 
817
  txtPermNumber->clear();
 
818
  txtPermNumber->insert(num);
 
819
  return;
 
820
}
 
821
 
 
822
void permGenerator::closeEvent( QCloseEvent *ce ){
 
823
  ce->accept();
 
824
  return;
 
825
}
 
826
 
 
827
void permGenerator::exitProgram() {
 
828
  exit(0);
 
829
  return;
 
830
}