GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
itkMegaCaptureImport.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #include "itkMegaCaptureImport.h"
36 
37 namespace itk
38 {
39 //--------------------------------------------------------------------------------
41 {
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 //--------------------------------------------------------------------------------
49 {
50 }
51 
52 //-----------------------------------------------------------------------------
53 
54 //-----------------------------------------------------------------------------
55 void
56 MegaCaptureImport::SetFileName(const std::string & iName)
57 {
58  if ( !m_FileName.empty() && !iName.empty() && m_FileName.compare(iName) != 0 )
59  {
60  return;
61  }
62  if ( iName.empty() && m_FileName.empty() )
63  {
64  return;
65  }
66  m_FileName = iName;
67  this->Modified();
68 }
69 
70 //-----------------------------------------------------------------------------
71 
72 //-----------------------------------------------------------------------------
73 void
75 {
76  size_t counter = 0;
77 
78  std::vector< std::string >::iterator nit;
79  for ( nit = m_FileNameS.begin();
80  nit != m_FileNameS.end();
81  ++nit )
82  {
83  GoFigureFileInfoHelper tempInfo;
84  tempInfo.m_Filename = ( *nit );
85 
86  std::string origFileName =
87  itksys::SystemTools::GetFilenameName( ( *nit ).c_str() );
88 
89  IntVectorType::reverse_iterator numGroupLengthItr =
90  m_StartAndLengthNumGroup.second.rbegin();
91 
92  IntVectorType::reverse_iterator numGroupStartItr =
93  m_StartAndLengthNumGroup.first.rbegin();
94 
95  std::vector< unsigned int > NumericalValues(m_NbSignificantMegaCaptureNumGroup, 0);
96 
97  int megaCaptureNumericalGroupCounter = 0;
98 
99  while ( ( numGroupLengthItr != m_StartAndLengthNumGroup.second.rend() )
100  && ( numGroupStartItr != m_StartAndLengthNumGroup.first.rend() )
101  && ( megaCaptureNumericalGroupCounter < m_NbSignificantMegaCaptureNumGroup ) )
102  {
103  std::string ValueAsString(
104  origFileName,
105  ( *numGroupStartItr ),
106  ( *numGroupLengthItr ) );
107 
108  int tempidx = m_NbSignificantMegaCaptureNumGroup - 1 - megaCaptureNumericalGroupCounter;
109 
110  NumericalValues[tempidx] = atoi( ValueAsString.c_str() );
111 
112  ++numGroupLengthItr;
113  ++numGroupStartItr;
114  ++megaCaptureNumericalGroupCounter;
115  } // end for each numerical group
116 
117  if ( m_HeaderFileName == "" )
118  {
119  // get the beginning of the filename to look for the headerfile:
120  // first, get back to the starting position of the last numerical values
121  // group which
122  // corresponds to the PL number:
123  numGroupStartItr = numGroupStartItr - 1;
124 
125  // then, substract the "PL-" from the position of the PL Number to find
126  // the position of the last character to be part of the headerfile name:
127  int LengthHeaderFile = *numGroupStartItr - 3;
128 
129  // get the headerfile name and add the .meg
130  std::string fileNamePath = itksys::SystemTools::GetFilenamePath(m_FileName);
131  m_HeaderFileName = fileNamePath + "/"
132  + origFileName.substr(0, LengthHeaderFile) + ".meg";
133  }
134 
136  {
137  NewMegaCaptureFile(tempInfo, NumericalValues);
138  m_OutputFileList.insert(tempInfo);
139  }
141  {
142  OldMegaCaptureFile(tempInfo, NumericalValues);
143  m_OutputFileList.insert(tempInfo);
144  }
145 
146  ++counter;
147  this->SetProgress( 0.25 +
148  0.75 * static_cast< float >( counter ) / static_cast< float >( m_FileNameS.size() ) );
149  } // end for each filename
150 
151  m_FileNameS.clear();
152 
153 }
154 
155 //-----------------------------------------------------------------------------
158 {
159  return m_OutputFileList;
160 }
161 
162 //-----------------------------------------------------------------------------
163 
164 //-----------------------------------------------------------------------------
165 void
167 {
168  Glob();
169  CreateOutput();
170 }
171 
172 //-----------------------------------------------------------------------------
173 
174 //-----------------------------------------------------------------------------
175 void
177 {
178  std::string FilenameModified = CleanFileName(m_FileName);
179 
181 
182  // create the regular expression to glob the entire set of file
183  std::string regExpFileName = FilenameModified;
184  IntVectorType::reverse_iterator numGroupLengthItr = m_StartAndLengthNumGroup.second.rbegin();
185  IntVectorType::reverse_iterator numGroupStartItr = m_StartAndLengthNumGroup.first.rbegin();
186 
188  {
190  }
191  else
192  {
194  }
195 
196  std::string regExpString = "([0-9]+)";
197  int megaCaptureNumericalGroupCounter = 0;
198 
199  while ( numGroupLengthItr != m_StartAndLengthNumGroup.second.rend()
200  && numGroupStartItr != m_StartAndLengthNumGroup.first.rend()
201  && megaCaptureNumericalGroupCounter < m_NbSignificantMegaCaptureNumGroup )
202  {
203  regExpFileName.replace(*numGroupStartItr, *numGroupLengthItr, regExpString);
204  ++numGroupLengthItr;
205  ++numGroupStartItr;
206  ++megaCaptureNumericalGroupCounter;
207  }
208 
209  this->SetProgress( 0.05 );
210 
211  // Include only filenames that exactly match this regular expression. Don't
212  // match filenames that have this string as a substring (ie. that have extra
213  // prefixes or suffixes).
214  regExpFileName = "^" + regExpFileName + "$";
215 
216  // Use a RegularExpressionSeriesFileNames to find the files to return
217  std::string unixArchetype = m_FileName;
218  itksys::SystemTools::ConvertToUnixSlashes(unixArchetype);
219 
220  std::string origFileName =
221  itksys::SystemTools::GetFilenameName( unixArchetype.c_str() );
222 
223  std::string fileNamePath =
224  itksys::SystemTools::GetFilenamePath( unixArchetype.c_str() );
225 
226  std::string pathPrefix;
227 
228  itk::RegularExpressionSeriesFileNames::Pointer fit =
229  itk::RegularExpressionSeriesFileNames::New();
230  fit->SetDirectory( fileNamePath.c_str() );
231  fit->SetRegularExpression( regExpFileName.c_str() );
232  fit->SetSubMatch(1);
233  fit->NumericSortOn();
234 
235  m_FileNameS = fit->GetFileNames();
236 
237  this->SetProgress( 0.15 );
238 
239  // re parse the indexes and length without the escape caracters
240  std::string::iterator s_start = origFileName.begin();
241  std::string::iterator s_end = origFileName.end();
242  std::string::iterator s_it = s_start;
243  int sIndex;
244 
245  while ( s_it != s_end )
246  {
247  // If the element is a number, find its starting index and length.
248  if ( ( *s_it ) >= '0' && ( *s_it ) <= '9' )
249  {
250  sIndex = static_cast< int >( s_it - s_start );
251  m_StartAndLengthNumGroup.first.push_back(sIndex);
252 
253  // Loop to one past the end of the group of numbers.
254  while ( s_it != origFileName.end() && ( *s_it ) >= '0' && ( *s_it ) <= '9' )
255  {
256  ++s_it;
257  }
258 
259  m_StartAndLengthNumGroup.second.push_back(static_cast< int >
260  ( s_it - s_start ) - sIndex);
261 
262  if ( s_it == s_end )
263  {
264  break;
265  }
266  }
267  ++s_it;
268  }
269 
270  this->SetProgress( 0.25 );
271 }
272 
273 //-----------------------------------------------------------------------------
274 
275 //-----------------------------------------------------------------------------
276 bool MegaCaptureImport::IsNewMegaCapture(const std::string & iFilename)
277 {
278  std::string FilenameModified = CleanFileName(iFilename);
279 
280  PairIntVectorType StartAndLengthNumericalGroup =
282 
283  return AreTheseNumericalGroupNewMegaCapture(StartAndLengthNumericalGroup);
284 }
285 
286 //-----------------------------------------------------------------------------
287 
288 //-----------------------------------------------------------------------------
290  MegaCaptureImport::PairIntVectorType StartAndLengthNumericalGroup)
291 {
292  return ( StartAndLengthNumericalGroup.first.size() >= 9 );
293 }
294 
295 //-----------------------------------------------------------------------------
296 
297 //-----------------------------------------------------------------------------
298 std::string MegaCaptureImport::CleanFileName(const std::string & iFilename)
299 {
300  // glob all jpeg file names
301  std::string unixArchetype = iFilename;
302 
303  itksys::SystemTools::ConvertToUnixSlashes(unixArchetype);
304  if ( itksys::SystemTools::FileIsDirectory( unixArchetype.c_str() ) )
305  {
306  // return type supposed to be one std::string!
307  std::cout << "iFilename is directory" << std::endl;
308  return std::string();
309 // return false;
310  }
311 
312  // Parse the fileNameName and fileNamePath
313  std::string origFileName =
314  itksys::SystemTools::GetFilenameName( unixArchetype.c_str() );
315  std::string fileNamePath =
316  itksys::SystemTools::GetFilenamePath( unixArchetype.c_str() );
317  std::string pathPrefix;
318  std::string ofileNameModified;
319  // "Clean" the filename by escaping any special characters with backslashes.
320  // This allows us to pass in filenames that include these special characters.
321  for ( unsigned int j = 0; j < origFileName.length(); j++ )
322  {
323  char oneChar = origFileName[j];
324  if (
325  oneChar == '^'
326  || oneChar == '$'
327  || oneChar == '.'
328  || oneChar == '['
329  || oneChar == ']'
330  || oneChar == '-'
331  || oneChar == '*'
332  || oneChar == '+'
333  || oneChar == '?'
334  || oneChar == '('
335  || oneChar == ')' )
336  {
337  ofileNameModified += "\\";
338  }
339  ofileNameModified += oneChar;
340  }
341 
342  // If there is no "/" in the name, the directory is not specified.
343  // In that case, use the default ".".
344  // This is necessary for the RegularExpressionSeriesFileNames.
345  if ( fileNamePath == "" )
346  {
347  fileNamePath = ".";
348  pathPrefix = "./";
349  }
350  else
351  {
352  pathPrefix = "";
353  }
354  return ofileNameModified;
355 }
356 
357 //-----------------------------------------------------------------------------
358 
359 //-----------------------------------------------------------------------------
362 {
363  // parse and keep it for ouput generation
364  PairIntVectorType oStartAndLength;
365 
366  std::string::const_iterator s_start = iFilename.begin();
367  std::string::const_iterator s_it = s_start;
368  std::string::const_iterator s_end = iFilename.end();
369 
370  int sIndex;
371 
372  while ( s_it != s_end )
373  {
374  // If the element is a number, find its starting index and length.
375  if ( ( *s_it ) >= '0' && ( *s_it ) <= '9' )
376  {
377  sIndex = static_cast< int >( s_it - s_start );
378 
379  //the first vector of oStartAndLength contains the index of all the
380  // starting numbers of
381  //the numerical group:
382  oStartAndLength.first.push_back(sIndex);
383 
384  // Loop to one past the end of the group of numbers.
385  while ( ( s_it != s_end ) && ( ( *s_it ) >= '0' ) && ( ( *s_it ) <= '9' ) )
386  {
387  ++s_it;
388  }
389  //the second vector of oStartAndLength contains the length of the
390  // numerical group:
391  oStartAndLength.second.push_back(
392  static_cast< int >( s_it - s_start ) - sIndex);
393 
394  if ( s_it == s_end )
395  {
396  break;
397  }
398  }
399  ++s_it;
400  }
401  return oStartAndLength;
402 }
403 
404 //-----------------------------------------------------------------------------
405 
406 //-----------------------------------------------------------------------------
408  const std::vector< unsigned int > & iNumericalValues)
409 {
410  ioTempInfo.m_CCoord = iNumericalValues[0];
411  ioTempInfo.m_RCoord = iNumericalValues[1];
412  ioTempInfo.m_YCoord = iNumericalValues[2];
413  ioTempInfo.m_XCoord = iNumericalValues[3];
414  ioTempInfo.m_TCoord = iNumericalValues[4];
415  ioTempInfo.m_ZCoord = iNumericalValues[5];
416 }
417 
418 //-----------------------------------------------------------------------------
419 
420 //-----------------------------------------------------------------------------
422  const std::vector< unsigned int > & iNumericalValues)
423 {
424  ioTempInfo.m_PCoord = iNumericalValues[0];
425  ioTempInfo.m_CCoord = iNumericalValues[1];
426  ioTempInfo.m_RCoord = iNumericalValues[2];
427  ioTempInfo.m_ZTileCoord = iNumericalValues[3];
428  ioTempInfo.m_YTileCoord = iNumericalValues[4];
429  ioTempInfo.m_XTileCoord = iNumericalValues[5];
430  ioTempInfo.m_TCoord = iNumericalValues[6];
431  ioTempInfo.m_Channel = iNumericalValues[7];
432  ioTempInfo.m_ZCoord = iNumericalValues[8];
433 }
434 
435 //-----------------------------------------------------------------------------
436 
437 //-----------------------------------------------------------------------------
439 {
440  return m_HeaderFileName;
441 }
442 
443 }
void NewMegaCaptureFile(GoFigureFileInfoHelper &ioTempInfo, const std::vector< unsigned int > &iNumericalValues)
Used for the database version2.
GoFigureFileInfoHelperMultiIndexContainer m_OutputFileList
GoFigureFileInfoHelperMultiIndexContainer GetOutput()
std::pair< IntVectorType, IntVectorType > PairIntVectorType
void OldMegaCaptureFile(GoFigureFileInfoHelper &ioTempInfo, const std::vector< unsigned int > &iNumericalValues)
Used for the database version1.
static std::string CleanFileName(const std::string &iFilename)
return a modified cleaned filename
static bool IsNewMegaCapture(const std::string &iFilename)
return true if the filename is of new megacapture format, false if it is the old one ...
static bool AreTheseNumericalGroupNewMegaCapture(PairIntVectorType StartAndLengthNumericalGroup)
boost::multi_index::multi_index_container< GoFigureFileInfoHelper, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_PCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_RCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_CCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_XTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_YTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_Channel >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_TCoord >, > >> GoFigureFileInfoHelperMultiIndexContainer
PairIntVectorType m_StartAndLengthNumGroup
static PairIntVectorType GetStartAndLengthOfNumericalGroupFilename(const std::string &iFilename)
return the list of the Index and the list of the length of all the numerical group present in the fil...
void SetFileName(const std::string &iName)