GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoDBImport.h
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 #ifndef __GoDBImport_h
36 #define __GoDBImport_h
37 
38 #include "vtkMySQLDatabase.h"
39 #include "ContourMeshStructure.h"
40 #include "ContourMeshContainer.h"
41 #include <vector>
42 #include <map>
43 
44 #include "QGoIOConfigure.h"
45 
52 class QGOIO_EXPORT GoDBImport
53 {
54 public:
55 
56  GoDBImport(std::string iServerName, std::string iLogin,
57  std::string iPassword, int iImagingSessionID,
58  std::string iFilename, int iCurrentTimePoint);
59 
60  virtual ~GoDBImport();
61 
67  void ImportContours();
68 
77  void ImportMeshes();
78 
86  void ImportTracks();
87 
90  std::vector< int > GetVectorNewMeshIDs()
91  {
92  return this->m_NewMeshIDs;
93  }
94 
97  std::vector< int > GetVectorNewContourIDs()
98  {
99  return this->m_NewContourIDs;
100  }
101 
104  std::vector< int > GetVectorNewTracksIDs()
105  {
106  return this->m_NewTracksIDs;
107  }
108 
113  {
114  return this->m_NewContourInfoForVisu;
115  }
116 
117 private:
118  vtkMySQLDatabase * m_DatabaseConnector;
119  std::string m_ServerName;
120  std::string m_Password;
121  std::string m_Login;
124  std::ifstream m_InFile;
125  std::vector< int > m_NewMeshIDs;
126  std::vector< int > m_NewContourIDs;
127  std::vector< int > m_NewTracksIDs;
128  std::vector< int > m_NewLineageIDs;
131 
132  typedef std::map< int, int > IntMapType;
133 
135  std::string FindFieldName(std::string iLine);
136 
139  std::string GetValueForTheLine(std::string iLine);
140 
142  bool IsLineForNumberOfEntities(std::string iLine);
143 
146  std::string SaveNoTracesEntities(IntMapType & ioMapColorIDs,
147  IntMapType & ioMapCellTypeIDs,
148  IntMapType & ioMapSubCellTypeIDs,
149  IntMapType & ioMapCoordIDs);
150 
151  void OpenDBConnection();
152 
153  void CloseDBConnection();
154 
159  void SaveTracesEntities(const IntMapType & iMapColorIDs,
160  const IntMapType & iMapCoordIDs,
161  const std::string & iLineContent,
162  const IntMapType & iMapCellTypeIDs,
163  const IntMapType & iMapSubCellTypeIDs,
164  bool SaveIntensities = false);
165 
168  //void FillContourInfoForVisu(
169  //std::vector<int> iListContourIDs);
170 
171  //void FillMeshInfoForVisu(std::vector<int> iListMeshIDs);
172 
173  void SaveIntensityForMesh(std::string & ioLineContent,
174  const IntMapType & iMapMeshIDs,
175  const IntMapType & iMapColorIDs);
176 
182  template< typename T >
183  std::string SaveImportedEntitiesInDatabase(int iNumberOfEntities,
184  IntMapType & ioMapMatchingIDs)
185  {
186  std::string LineContent;
187 
188 #ifdef HAS_OPENMP
189 #pragma omp for
190 #endif
191  for ( int i = 0; i < iNumberOfEntities; i++ )
192  {
193  T EntityToSave;
194  LineContent = this->GetValuesFromInfile< T >(EntityToSave);
195  int OldID = atoi( EntityToSave.GetMapValue( EntityToSave.GetTableIDName() ).c_str() );
196  if (OldID > 0) // in case their is an error in the file to import
197  {
198  EntityToSave.SetField(EntityToSave.GetTableIDName(), "0");
199  int NewID = EntityToSave.SaveInDB(this->m_DatabaseConnector);
200  ioMapMatchingIDs[OldID] = NewID;
201  }
202  }
203  return LineContent;
204  }
205 
208  template< typename T >
209  std::string GetValuesFromInfile(T & ioEntityToFill)
210  {
211  std::string LineContent;
212 
213  getline(this->m_InFile, LineContent);
214  std::string FieldName = this->FindFieldName(LineContent);
215  std::string ValueForField = this->GetValueForTheLine(LineContent);
216  while ( ValueForField != "NoValueOnTheLine" )
217  {
218  ioEntityToFill.SetField(FieldName, ValueForField);
219  getline(this->m_InFile, LineContent);
220  ValueForField = this->GetValueForTheLine(LineContent);
221  FieldName = this->FindFieldName(LineContent);
222  }
223  //skip the line </NameOfEntity>:
224  getline(this->m_InFile, LineContent);
225  return LineContent;
226  }
227 
231  template< typename T >
232  void ReplaceTheFieldWithNewIDs(const IntMapType & iMapIDs,
233  std::string iFieldName, T & ioEntity)
234  {
235  typename IntMapType::const_iterator iter =
236  iMapIDs.find( atoi( ioEntity.GetMapValue(iFieldName).c_str() ) );
237  //in case the value of the field name is 0 which corresponds to
238  //an not yet associated value, it won't be found in the map:
239  if ( iter == iMapIDs.end() )
240  {
241  return;
242  }
243  int NewID = iter->second;
244  ioEntity.SetField(iFieldName, NewID);
245  }
246 
251  template< typename T >
252  void ReplaceCommonFieldsForTraces(T & ioEntityToSave,
253  const IntMapType & iMapColorIDs,
254  const IntMapType & iMapCoordIDs,
255  const IntMapType & iMapCollectionIDs)
256  {
257  ioEntityToSave.SetField(
258  "ImagingSessionID", this->m_ImagingSessionID);
259  this->ReplaceTheFieldWithNewIDs< T >(
260  iMapColorIDs, "ColorID", ioEntityToSave);
261  this->ReplaceTheFieldWithNewIDs< T >(
262  iMapCoordIDs, "CoordIDMax", ioEntityToSave);
263  this->ReplaceTheFieldWithNewIDs< T >(
264  iMapCoordIDs, "CoordIDMin", ioEntityToSave);
265  if ( ioEntityToSave.GetCollectionIDName() != "NoneID" )
266  {
267  this->ReplaceTheFieldWithNewIDs< T >(
268  iMapCollectionIDs, ioEntityToSave.GetCollectionIDName(),
269  ioEntityToSave);
270  }
271  }
272 
275  template< typename T >
276  void SaveTraces(const IntMapType & iMapColorIDs,
277  const IntMapType & iMapCoordIDs,
278  const IntMapType & iMapCollectionIDs,
279  std::string & ioLineContent,
280  std::vector< int > & ioNewTracesIDs,
281  IntMapType & ioMapTraceIDs,
282  const IntMapType & iMapIDsSpecificOne,
283  const IntMapType & iMapIDsSpecificTwo
284  )
285  {
286  T TraceToSave;
287  int NumberOfTraces = atoi( this->GetValueForTheLine(ioLineContent).c_str() );
288 
289  getline(this->m_InFile, ioLineContent);
290 
291 #ifdef HAS_OPENMP
292 #pragma omp for
293 #endif
294  for ( int i = 0; i < NumberOfTraces; i++ )
295  {
296  ioLineContent = this->GetValuesFromInfile< T >(
297  TraceToSave);
298  //for mesh, need to get the new celltype/subcelltype:
299  if ( TraceToSave.GetTableName() == "mesh" )
300  {
301  if ( !iMapIDsSpecificOne.empty() )
302  {
303  this->ReplaceTheFieldWithNewIDs< T >(
304  iMapIDsSpecificOne, "CellTypeID", TraceToSave);
305  }
306  if ( !iMapIDsSpecificTwo.empty() )
307  {
308  this->ReplaceTheFieldWithNewIDs< T >(
309  iMapIDsSpecificTwo, "SubCellularID", TraceToSave);
310  }
311  }
312  this->ReplaceCommonFieldsForTraces(
313  TraceToSave, iMapColorIDs, iMapCoordIDs, iMapCollectionIDs);
314  int OldTraceID = atoi( TraceToSave.GetMapValue( TraceToSave.GetTableIDName() ).c_str() );
315  /*in order the query works, the TraceID to be saved has to be set to 0 otherwise
316  if the TraceID already exists,the query will return the error
317  "Duplicate entry TraceID for key primary":*/
318  TraceToSave.SetField(TraceToSave.GetTableIDName(), "0");
319  int NewTraceID = TraceToSave.DoesThisBoundingBoxExist(this->m_DatabaseConnector);
320  if ( NewTraceID == -1 )
321  {
322  NewTraceID = TraceToSave.SaveInDB(this->m_DatabaseConnector);
323  // this->m_NewTraceIDs.push_back(NewTraceID);
324  }
325  else
326  {
327  std::cout << "The trace" << OldTraceID << " has the same bounding box as ";
328  std::cout << "the existing trace " << NewTraceID;
329  std::cout << "so the imported contours belonging to the mesh " << OldTraceID;
330  std::cout << " will belong to the existing mesh " << NewTraceID << std::endl;
331  }
332  ioNewTracesIDs.push_back(NewTraceID);
333  ioMapTraceIDs[OldTraceID] = NewTraceID;
334  }
335  }
336 };
337 #endif
std::string m_Login
Definition: GoDBImport.h:121
std::vector< int > GetVectorNewContourIDs()
return a vector of the IDs for the contours read from the import file and saved in the database ...
Definition: GoDBImport.h:97
std::string m_ServerName
Definition: GoDBImport.h:119
std::vector< int > m_NewLineageIDs
Definition: GoDBImport.h:128
std::string SaveImportedEntitiesInDatabase(int iNumberOfEntities, IntMapType &ioMapMatchingIDs)
get the values from the import file,save the corresponding number of entities in the database...
Definition: GoDBImport.h:183
void ReplaceTheFieldWithNewIDs(const IntMapType &iMapIDs, std::string iFieldName, T &ioEntity)
replace in the entity to be saved the fieldname with the new IDs created that matches the old one in ...
Definition: GoDBImport.h:232
std::vector< int > m_NewTracksIDs
Definition: GoDBImport.h:127
std::vector< int > GetVectorNewMeshIDs()
return a vector of the IDs for the meshes read from the import file and saved in the database ...
Definition: GoDBImport.h:90
std::string m_Password
Definition: GoDBImport.h:120
std::vector< int > m_NewContourIDs
Definition: GoDBImport.h:126
std::map< int, int > IntMapType
Definition: GoDBImport.h:132
std::string GetValuesFromInfile(T &ioEntityToFill)
Get the values from the import File to fill the corresponding GoDBRow.
Definition: GoDBImport.h:209
ContourMeshContainer * m_NewMeshInfoForVisu
Definition: GoDBImport.h:130
ContourMeshContainer * GetNewContourInfo()
return a vector of the info needed to add in the visu the contours read from the import file and save...
Definition: GoDBImport.h:112
std::vector< int > m_NewMeshIDs
Definition: GoDBImport.h:125
ContourMeshContainer * m_NewContourInfoForVisu
Definition: GoDBImport.h:129
This class get the data of traces from a textfile and save them into the GoFigure Database...
Definition: GoDBImport.h:52
vtkMySQLDatabase * m_DatabaseConnector
Definition: GoDBImport.h:118
Wraps a boost::multi_index_container of ContourMeshStructure. This class intends to synchronize Conto...
void ReplaceCommonFieldsForTraces(T &ioEntityToSave, const IntMapType &iMapColorIDs, const IntMapType &iMapCoordIDs, const IntMapType &iMapCollectionIDs)
replace old IDs found in the import file with new created IDs in the trace to be saved for common fie...
Definition: GoDBImport.h:252
int m_ImagingSessionID
Definition: GoDBImport.h:122
void SaveTraces(const IntMapType &iMapColorIDs, const IntMapType &iMapCoordIDs, const IntMapType &iMapCollectionIDs, std::string &ioLineContent, std::vector< int > &ioNewTracesIDs, IntMapType &ioMapTraceIDs, const IntMapType &iMapIDsSpecificOne, const IntMapType &iMapIDsSpecificTwo)
Definition: GoDBImport.h:276
int m_CurrentTimePoint
Definition: GoDBImport.h:123
std::vector< int > GetVectorNewTracksIDs()
return a vector of the IDs for the tracks read from the the import file and saved in the database ...
Definition: GoDBImport.h:104
std::ifstream m_InFile
Definition: GoDBImport.h:124