File: CommandConvertDataFileToCaret6.cxx

package info (click to toggle)
caret 5.6.1.3~dfsg.1-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 23,352 kB
  • ctags: 27,394
  • sloc: cpp: 363,314; python: 6,718; ansic: 5,507; makefile: 227; sh: 9
file content (271 lines) | stat: -rw-r--r-- 9,335 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

/*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 <QDateTime>
#include <QDir>
#include <QVector>
#include <CaretVersion.h>

#include "AbstractFile.h"
#include "AreaColorFile.h"
#include "BorderFile.h"
#include "BorderProjectionFile.h"
#include "BrainModelSurface.h"
#include "BrainSet.h"
#include "CommandConvertDataFileToCaret6.h"
#include "FileFilters.h"
#include "FileUtilities.h"
#include "FociFile.h"
#include "FociProjectionFile.h"
#include "PaintFile.h"
#include "ProgramParameters.h"
#include "ScriptBuilderParameters.h"
#include "SpecFile.h"
#include "Structure.h"
#include "TopologyFile.h"
#include "VolumeFile.h"

/**
 * constructor.
 */
CommandConvertDataFileToCaret6::CommandConvertDataFileToCaret6()
   : CommandBase("-caret6-convert-data-file",
                 "CONVERT DATA FILE TO CARET6 FORMAT")
{
}

/**
 * destructor.
 */
CommandConvertDataFileToCaret6::~CommandConvertDataFileToCaret6()
{
}

/**
 * get the script builder parameters.
 */
void
CommandConvertDataFileToCaret6::getScriptBuilderParameters(ScriptBuilderParameters& paramsOut) const
{
   paramsOut.clear();
}

/**
 * get full help information.
 */
QString
CommandConvertDataFileToCaret6::getHelpInformation() const
{
   QString helpInfo =
      (indent3 + getShortDescription() + "\n"
       + indent6 + parameters->getProgramNameWithoutPath() + " " + getOperationSwitch() + "  \n"
       + indent9 + "[-allow-overwrite]\n"
       + indent9 + "[-structure  structure-name]\n"
       + indent9 + "[-area-color-file    area-color-file-name]\n"
       + indent9 + "[-border-color-file  border-color-file-name]\n"
       + indent9 + "[-foci-color-file    foci-color-file-name]\n"
       + indent9 + "<names-of-files-for-conversion>]\n"
       + indent9 + "\n"
       + indent9 + "Since Caret6 expects colors to be within the border, foci\n"
       + indent9 + ", label, and label volume files, the appropriate color\n"
       + indent9 + "file should be specified using its color file option. \n"
       + indent9 + "This results in the colors being added into the border,\n"
       + indent9 + "foci, label, or label volume file.\n"
       + indent9 + "\n"
       + indent9 + "It is highly recommended that the structure be specified\n"
       + indent9 + "as many Caret6 data files are structure dependent.\n"
       + indent9 + "\"structure-name\" is one of:\n"
       + indent9 + "   right\n"
       + indent9 + "   left\n"
       + indent9 + "   cerebellum\n"
       + indent9 + "\n"
       + indent9 + "By default, files are not overwritten.  To allow \n"
       + indent9 + "the overwrite of exisiting files, the \"-allow-overwrite\"\n"
       + indent9 + "option must be added to the command.\n"
       + indent9 + "\n"
       + indent9 + "This command ALWAYS writes files to the current directory.\n"
       + indent9 + "IT IS BEST TO RUN THIS COMMAND FROM A DIRECTORY DIFFERENT\n"
       + indent9 + "THAN THE DIRECTORY THE INPUT FILE IS LOCATED.  Running\n"
       + indent9 + "the command in this manner, will prevent the original files\n"
       + indent9 + "from being corrupted if the command fails.\n"
       + indent9 + "\n"
       + indent9 + "In most cases, you are better off converting a spec file\n"
       + indent9 + "and its data file to Caret6 format using \n"
       + indent9 + "   \"caret_command -convert-spec-file-to-caret6\" as it\n"
       + indent9 + "    will convert coordinate and topology files into \n"
       + indent9 + "   surface files and create a spec file that is Caret6 \n"
       + indent9 + "   compatible.\n"
       + indent9 + "\n");

   return helpInfo;
}

/**
 * execute the command.
 */
void
CommandConvertDataFileToCaret6::executeCommand() throw (BrainModelAlgorithmException,
                                     CommandException,
                                     FileException,
                                     ProgramParametersException,
                                     StatisticException)
{
   AbstractFile::setOverwriteExistingFilesAllowed(false);
   Structure structure;
   QVector<QString> filenames;
   QVector<QString> areaColorFileNames;
   QVector<QString> borderColorFileNames;
   QVector<QString> fociColorFileNames;

   while (parameters->getParametersAvailable()) {
      const QString paramName =
        parameters->getNextParameterAsString("Convert to Caret6 Parameter");

      if (paramName == "-allow-overwrite") {
         AbstractFile::setOverwriteExistingFilesAllowed(true);
      }
      else if (paramName == "-area-color-file") {
         areaColorFileNames.push_back(parameters->getNextParameterAsString("Area Color File Name"));
      }
      else if (paramName == "-border-color-file") {
         borderColorFileNames.push_back(parameters->getNextParameterAsString("Border Color File Name"));
      }
      else if (paramName == "-foci-color-file") {
         fociColorFileNames.push_back(parameters->getNextParameterAsString("Foci Color File Name"));
      }
      else if (paramName == "-structure") {
         QString structureName = parameters->getNextParameterAsString("Structure Name").toLower();
         if (structureName == "left") {
            structure.setType(Structure::STRUCTURE_TYPE_CORTEX_LEFT);
         }
         else if (structureName == "right") {
            structure.setType(Structure::STRUCTURE_TYPE_CORTEX_RIGHT);
         }
         else if (structureName == "cerebellum") {
            structure.setType(Structure::STRUCTURE_TYPE_CEREBELLUM);
         }
         else {
            throw CommandException("Invalid structure name: " + structureName);
         }
      }
      else {
         filenames.push_back(paramName);
      }
   }

   int numDataFiles = static_cast<int>(filenames.size());

   if (numDataFiles <= 0) {
      throw CommandException("No files were specified for conversion");
   }

   if (areaColorFileNames.size() > 0) {
      for (int i = 0; i < areaColorFileNames.size(); i++) {
         AreaColorFile acf;
         acf.readFile(areaColorFileNames[i]);
         this->areaColorFile.append(acf);
      }
   }

   if (borderColorFileNames.size() > 0) {
      for (int i = 0; i < borderColorFileNames.size(); i++) {
         BorderColorFile bcf;
         bcf.readFile(borderColorFileNames[i]);
         this->borderColorFile.append(bcf);
      }
   }

   if (fociColorFileNames.size() > 0) {
      for (int i = 0; i < fociColorFileNames.size(); i++) {
         FociColorFile fcf;
         fcf.readFile(fociColorFileNames[i]);
         this->fociColorFile.append(fcf);
      }
   }

   for (int i = 0; i < numDataFiles; i++) {
      QString inputFileName = filenames.at(i);
      QString outputFileName = this->convertFile(inputFileName, structure);
   }
}

/**
 *  Convert a file.
 */
QString
CommandConvertDataFileToCaret6::convertFile(const QString& inputFileName,
                                    const Structure& structure)
{
   QString errorMessage;
   QString outputFileName;
   AbstractFile* af = AbstractFile::readAnySubClassDataFile(inputFileName, false, errorMessage);
   if (af != NULL) {
      ColorFile* colorFile = NULL;
      if ((dynamic_cast<PaintFile*>(af) != NULL) ||
          (dynamic_cast<VolumeFile*>(af) != NULL)) {
         colorFile = &this->areaColorFile;
      }
      else if ((dynamic_cast<BorderFile*>(af) != NULL) ||
               (dynamic_cast<BorderProjectionFile*>(af) != NULL)) {
         colorFile = &this->borderColorFile;
      }
      else if ((dynamic_cast<FociFile*>(af) != NULL) ||
               (dynamic_cast<FociProjectionFile*>(af) != NULL)) {
         colorFile = &this->fociColorFile;
      }
      else if (dynamic_cast<VolumeFile*>(af) != NULL) {
         colorFile = &this->areaColorFile;
      }

      af->updateMetaDataForCaret6();
      
      QString dataFileName = FileUtilities::basename(inputFileName);
      try {
         outputFileName =
              af->writeFileInCaret6Format(dataFileName, structure, colorFile, true);
         std::cout << inputFileName.toAscii().constData() << " OK" << std::endl;
      }
      catch (FileException e) {
         std::cout << " ERROR CONVERTING "
                   << dataFileName.toAscii().constData()
                   << ": "
                   << e.whatQString().toAscii().constData()
                   << std::endl;
      }
   }
   else {
      std::cout << " WARNING, UNRECOGNIZED file type for file: "
                << inputFileName.toAscii().constData()
                << std::endl;
   }
   
   return outputFileName;
}