~ubuntu-branches/ubuntu/saucy/caret/saucy-proposed

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
/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET is free software you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CARET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/

#include <iostream>

#include "GeodesicDistanceFile.h"
#include "SpecFile.h"
#include "StringUtilities.h"

/**
 * Constructor.
 */
GeodesicDistanceFile::GeodesicDistanceFile() :
   NodeAttributeFile("geodesic distance file",
                     SpecFile::getGeodesicDistanceFileExtension(),
                     AbstractFile::FILE_FORMAT_ASCII,
                     FILE_IO_READ_AND_WRITE, 
                     FILE_IO_READ_AND_WRITE, 
                     FILE_IO_NONE, 
                     FILE_IO_NONE)
{
   clear();
}

/**
 * Destructor.
 */
GeodesicDistanceFile::~GeodesicDistanceFile()
{
   clear();
}

/**
 * append a node attribute  file to this one.
 */
void 
GeodesicDistanceFile::append(NodeAttributeFile& naf) throw (FileException)
{
   if (naf.getNumberOfColumns() > 0) {
      std::vector<int> destinationColumns(naf.getNumberOfColumns(), APPEND_COLUMN_NEW);
      append(naf, destinationColumns, FILE_COMMENT_MODE_APPEND);
   }
}

/**
 * append a node attribute file to this one but selectively load/overwrite columns
 * columnDestination is where naf's columns should be (-1=new, -2=do not load).
 */
void 
GeodesicDistanceFile::append(NodeAttributeFile& naf, 
                             std::vector<int> columnDestinationIn,
                             const FILE_COMMENT_MODE fcm) throw (FileException)
{
   bool setTheFileNameFlag = false;
   
   std::vector<int> columnDestination = columnDestinationIn;
   
   GeodesicDistanceFile& gdf = dynamic_cast<GeodesicDistanceFile&>(naf);
   
   if (numberOfNodes != gdf.numberOfNodes) {
      if (numberOfNodes > 0) {
         throw FileException("Trying to append geodesic distance file with a different number "
                          "of nodes");
      }
      else {
         setTheFileNameFlag = true;
      }
   }

   setModified();
   
   //
   // Find out how many columns need to be added
   //   
   int numColumnsToAdd = 0;
   int newColumnIndex = numberOfColumns;
   for (int i = 0; i < gdf.getNumberOfColumns(); i++) {
      if (columnDestination[i] == APPEND_COLUMN_NEW) {
         numColumnsToAdd++;
         columnDestination[i] = newColumnIndex;
         newColumnIndex++;
      }
   }
   //
   // Add on additional columns
   //
   if (getNumberOfNodes() == 0) {
      setNumberOfNodesAndColumns(gdf.getNumberOfNodes(), numColumnsToAdd);
   }
   else {
      addColumns(numColumnsToAdd);
   }
      
   //
   // copy column names from other file
   //
   for (int k = 0; k < gdf.numberOfColumns; k++) {
      if (columnDestination[k] >= 0) {
         const int col = columnDestination[k];
         setColumnName(col, gdf.getColumnName(k));
         setColumnComment(col, gdf.getColumnComment(k));
      }
   }
   
   //
   // copy geodesic data from other file
   //
   for (int j = 0; j < gdf.numberOfColumns; j++) {
      if (columnDestination[j] >= 0) {
         const int col = columnDestination[j];
         for (int i = 0; i < numberOfNodes; i++) {
            setNodeParent(i, col, gdf.getNodeParent(i, j));
            setNodeParentDistance(i, col, getNodeParentDistance(i, j));
         }
         setRootNode(col, gdf.getRootNode(j));
      }
   }
   
   if (setTheFileNameFlag) {
      setFileName(gdf.getFileName());
   }
   
   //
   // transfer the file's comment
   //
   appendFileComment(gdf, fcm);
}

/**
 * add columns to this node attribute file.
 */
void 
GeodesicDistanceFile::addColumns(const int numberOfNewColumns)
{
   const int oldNumberOfColumns = numberOfColumns;
   const int totalColumns = numberOfColumns + numberOfNewColumns;
   
   //
   // Save existing lat lon data
   //
   const std::vector<int>     parents = nodeParent;
   const std::vector<float> distances = nodeParentDistance;
   
   //
   // Setup file for new number of columns (will expand space for column naming)
   //
   setNumberOfNodesAndColumns(numberOfNodes, totalColumns);
   
   //
   // transfer existing data
   //
   for (int i = 0; i < numberOfNodes; i++) {
      for (int j = 0; j < numberOfColumns; j++) {
         if (j < oldNumberOfColumns) {
            const int oldIndex = (oldNumberOfColumns * i) + j;
            setNodeParent(i, j, parents[oldIndex]);
            setNodeParentDistance(i, j, distances[oldIndex]);
         }
         else {
            setNodeParent(i, j, -1);
            setNodeParentDistance(i, j, 0.0);
         }
      }
   }
      
   setModified();
}

/**
 * add nodes to this file.
 */
void 
GeodesicDistanceFile::addNodes(const int numberOfNodesToAdd)
{
   setNumberOfNodesAndColumns(numberOfNodes + numberOfNodesToAdd, numberOfColumns);   
}

/**
 * Clears current file data in memory. 
 */
void 
GeodesicDistanceFile::clear()
{
   clearNodeAttributeFile();
   setNumberOfNodesAndColumns(0, 0);
}

/** 
 * deform "this" node attribute file placing the output in "deformedFile".
 */
void 
GeodesicDistanceFile::deformFile(const DeformationMapFile& /*dmf*/, 
                    NodeAttributeFile& /*deformedFile*/,
                    const DEFORM_TYPE /*dt*/) const throw (FileException)
{
   throw FileException("Deformation of Geodesic Distance File not supported.");
}

/**
 * set the number of nodes and columns in the file.
 */
void 
GeodesicDistanceFile::setNumberOfNodesAndColumns(const int numNodes, const int numCols)
{
   numberOfNodes = numNodes;
   numberOfColumns = numCols;
   
   const int num = numberOfNodes * numberOfColumns * numberOfItemsPerColumn;
   if (num <= 0) {
      nodeParent.clear();
      nodeParentDistance.clear();
      rootNode.clear();
   }
   else {
      nodeParent.resize(num);
      nodeParentDistance.resize(num);
      rootNode.resize(numberOfColumns, -1);
   }
   
   numberOfNodesColumnsChanged();
}

/**
 * reset a column of data.
 */
void 
GeodesicDistanceFile::resetColumn(const int columnNumber)
{
   for (int i = 0; i < numberOfNodes; i++) {
      setNodeParent(i, columnNumber, -1);
      setNodeParentDistance(i, columnNumber, 0.0);
   }
   rootNode[columnNumber] = -1;
   setModified();
}

/**
 * remove a column of data.
 */
void 
GeodesicDistanceFile::removeColumn(const int columnNumber)
{
   if (numberOfColumns <= 1) {
      clear();
      return;
   }
   
   //
   // Transfer lat/lon data
   //
   GeodesicDistanceFile gdf;
   gdf.setNumberOfNodesAndColumns(numberOfNodes, numberOfColumns - 1);
   for (int i = 0; i < numberOfNodes; i++) {
      int ctr = 0;
      for (int j = 0; j < numberOfColumns; j++) {
         if (j != columnNumber) {
            gdf.setNodeParent(i, ctr, getNodeParent(i, j));
            gdf.setNodeParentDistance(i, ctr, getNodeParentDistance(i, j));
            ctr++;
         }
      }
   }
   int colCtr = 0;
   for (int j = 0; j < numberOfColumns; j++) {
      if (j != columnNumber) {
         gdf.setRootNode(colCtr, getRootNode(j));
         colCtr++;
      }
   }
   
   //
   // Transfer column information
   //
   int ctr = 0;
   for (int j = 0; j < numberOfColumns; j++) {
      if (j != columnNumber) {
         setColumnName(ctr, getColumnName(j));
         setColumnComment(ctr, getColumnComment(j));
         ctr++;
      }
   }
   
   setNumberOfNodesAndColumns(numberOfNodes, numberOfColumns - 1);
   nodeParent = gdf.nodeParent;
   nodeParentDistance = gdf.nodeParentDistance;
   rootNode = gdf.rootNode;

   setModified();
}

/**
 * get the parent for a node.
 */
int 
GeodesicDistanceFile::getNodeParent(const int nodeNumber, const int columnNumber) const
{
   const int index = getOffset(nodeNumber, columnNumber);
   return nodeParent[index];
}

/** 
 * set the parent for a node.
 */
void 
GeodesicDistanceFile::setNodeParent(const int nodeNumber, const int columnNumber,
                                    const int parent)
{
   const int index = getOffset(nodeNumber, columnNumber);
   nodeParent[index] = parent;
}
                   
/**
 * get the distance to a parent for a node.
 */
float 
GeodesicDistanceFile::getNodeParentDistance(const int nodeNumber, const int columnNumber) const
{
   const int index = getOffset(nodeNumber, columnNumber);
   return nodeParentDistance[index];
}

/**
 * set the distance to a parent for a node.
 */
void 
GeodesicDistanceFile::setNodeParentDistance(const int nodeNumber, const int columnNumber,
                                            const float distance)
{
   const int index = getOffset(nodeNumber, columnNumber);
   nodeParentDistance[index] = distance;
}

/**
 * get the root node.
 */
int 
GeodesicDistanceFile::getRootNode(const int columnNumber) const
{
   return rootNode[columnNumber];
}

/**
 * set the root node.
 */
void 
GeodesicDistanceFile::setRootNode(const int columnNumber, const int node)
{
   rootNode[columnNumber] = node;
}
      
/**
 * Read the contents of the file (header has already been read).
 */
void 
GeodesicDistanceFile::readFileData(QFile& /*file*/, 
                          QTextStream& stream,
                          QDataStream& binStream,
                                  QDomElement& /* rootElement */) throw (FileException)
{
   int numNodes = -1;
   int numCols  = -1;
   int fileVersion = -1;
   
   bool readingTags = true;
   while(readingTags) {
      QString tag, tagValue;
      readTagLine(stream, tag, tagValue);
      
      if (tag == tagBeginData) {
         readingTags = false;
      }
      else if (tag == tagFileVersion) {
         fileVersion = tagValue.toInt();
      }
      else if (tag == tagNumberOfNodes) {
         numNodes = tagValue.toInt();
         if ((numNodes > 0) && (numCols > 0)) {
            setNumberOfNodesAndColumns(numNodes, numCols);
         }
      }
      else if (tag == tagNumberOfColumns) {
         numCols = tagValue.toInt();
         if ((numNodes > 0) && (numCols > 0)) {
            setNumberOfNodesAndColumns(numNodes, numCols);
         }
      }
      else if (tag == tagColumnName) {
         QString name;
         const int index = splitTagIntoColumnAndValue(tagValue, name);
         columnNames[index] = name;
      }
      else if (tag == tagColumnComment) {
         QString name;
         const int index = splitTagIntoColumnAndValue(tagValue, name);
         columnComments[index] = StringUtilities::setupCommentForDisplay(name);
      }
      else if (tag == tagFileTitle) {
         fileTitle = tagValue;
      }
      else {
         std::cout << "WARNING: Unknown Geodesic File Tag: " << tag.toAscii().constData() << std::endl;
      }
   }

   if (fileVersion != 1) {
      QString msg("Geodesic Distance File Version ");
      msg.append(StringUtilities::fromNumber(fileVersion));
      msg.append(" is not supported in this version of Caret5.  You may need a newer "
                 "version of Caret5.");
      throw FileException(filename, msg);
   }
   
   if (numNodes <= 0) {
      throw FileException(filename, "No data in Geodesic file");
   }
   
   //
   // Should reading data be skipped ?
   //
   if (getReadMetaDataOnlyFlag()) {
      return;
   }

   switch (getFileReadType()) {
      case FILE_FORMAT_ASCII:
         {
            std::vector<QString> tokens;
            QString line;
            
            readLineIntoTokens(stream, line, tokens);
            if (static_cast<int>(tokens.size()) >= numberOfColumns) {
               for (int j = 0; j < numberOfColumns; j++) {
                  rootNode[j] = tokens[j].toInt();
               }
            }
            
            for (int i = 0; i < numberOfNodes; i++) {
               readLineIntoTokens(stream, line, tokens);
               if (static_cast<int>(tokens.size()) == (numberOfColumns * 2 + 1)) {
                  for (int j = 0; j < numberOfColumns; j++) {
                     const int index  = getOffset(i, j);
                     nodeParent[index]  = tokens[j * 2 + 1].toInt();
                     nodeParentDistance[index] = tokens[j * 2 + 2].toFloat();
                  }
               }
               else {
                  QString msg("Reading Geodesic Distance file line: ");
                  msg.append(line);
                  throw FileException(filename, msg);
               }
            }   
         }
         break;
      case FILE_FORMAT_BINARY:
         //
         // Needed for QT 4.2.2
         //
         binStream.device()->seek(stream.pos());
         for (int j = 0; j < numCols; j++) {
            binStream >> rootNode[j];
         }
         
         for (int i = 0; i < numNodes; i++) {
            for (int j = 0; j < numCols; j++) {
               int parent;
               float distance;
               binStream >> parent
                         >> distance;
               const int index  = getOffset(i, j);
               nodeParent[index]  = parent;
               nodeParentDistance[index] = distance;
            }
         }
         break;
      case FILE_FORMAT_XML:
         throw FileException(filename, "Reading in XML format not supported.");
      case FILE_FORMAT_XML_BASE64:
         throw FileException(filename, "Reading XML Base64 not supported.");
         break;
      case FILE_FORMAT_XML_GZIP_BASE64:
         throw FileException(filename, "Reading XML GZip Base64 not supported.");
         break;
      case FILE_FORMAT_OTHER:
         throw FileException(filename, "Reading in Other format not supported.");
         break;
      case FILE_FORMAT_COMMA_SEPARATED_VALUE_FILE:
         throw FileException(filename, "Comma Separated Value File Format not supported.");
         break;
   }
}

/**
 * Write the file's data (header has already been written).
 */
void 
GeodesicDistanceFile::writeFileData(QTextStream& stream,
                           QDataStream& binStream,
                                 QDomDocument& /* xmlDoc */,
                                  QDomElement& /* rootElement */) throw (FileException)
{
   const int numNodes = getNumberOfNodes();
   const int numCols  = getNumberOfColumns();
   
   stream << tagFileVersion << " 1" << "\n";
   stream << tagNumberOfNodes << " " << numNodes << "\n";
   stream << tagNumberOfColumns << " " << numCols << "\n";
   stream << tagFileTitle << " " << fileTitle << "\n";
   for (int j = 0; j < numberOfColumns; j++) {
      stream << tagColumnName << " " << j << " " << columnNames[j] << "\n";
      stream << tagColumnComment << " " << j 
             << " " << StringUtilities::setupCommentForStorage(columnComments[j]) << "\n";
   }
   stream << tagBeginData << "\n";
   
   switch (getFileWriteType()) {
      case FILE_FORMAT_ASCII:
         for (int j = 0; j < numCols; j++) {
            if (j > 0) {
               stream << " ";
            }
            stream << rootNode[j];
         }
         stream << "\n";
         
         for (int i = 0; i < numNodes; i++) {
            stream << i;
            for (int j = 0; j < numCols; j++) {
               stream << " "
                     << getNodeParent(i, j) << " "
                     << getNodeParentDistance(i, j);
            }
            stream << "\n";
         }
         break;
      case FILE_FORMAT_BINARY:
#ifdef QT4_FILE_POS_BUG
         setBinaryFilePosQT4Bug();
#else  // QT4_FILE_POS_BUG
         //
         // still a bug in QT 4.2.2
         //
         setBinaryFilePosQT4Bug();
#endif // QT4_FILE_POS_BUG
         for (int j = 0; j < numCols; j++) {
            binStream << rootNode[j];
         }
         
         for (int i = 0; i < numNodes; i++) {
            for (int j = 0; j < numCols; j++) {
               binStream << getNodeParent(i, j)
                         << getNodeParentDistance(i, j);
            }
         }
         break;
      case FILE_FORMAT_XML:
         throw FileException(filename, "Writing in XML format not supported.");
         break;
      case FILE_FORMAT_XML_BASE64:
         throw FileException(filename, "XML Base64 not supported.");
         break;
      case FILE_FORMAT_XML_GZIP_BASE64:
         throw FileException(filename, "XML GZip Base64 not supported.");
         break;
      case FILE_FORMAT_OTHER:
         throw FileException(filename, "Writing in Other format not supported.");
         break;
      case FILE_FORMAT_COMMA_SEPARATED_VALUE_FILE:
         throw FileException(filename, "Comma Separated Value File Format not supported.");
         break;
   }
}