GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoDBImport.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 "GoDBImport.h"
37 #include "QueryDataBaseHelper.h"
38 #include "ConvertToStringHelper.h"
39 #include "GoDBColorRow.h"
40 #include "GoDBCellTypeRow.h"
41 #include "GoDBSubCellTypeRow.h"
42 #include "GoDBCoordinateRow.h"
43 #include "GoDBContourRow.h"
44 #include "GoDBMeshRow.h"
45 #include "GoDBTrackRow.h"
46 #include "GoDBLineageRow.h"
47 #include "GoDBChannelRow.h"
48 #include "GoDBIntensityRow.h"
49 
50 //--------------------------------------------------------------------------
51 GoDBImport::GoDBImport(std::string iServerName, std::string iLogin,
52  std::string iPassword, int iImagingSessionID,
53  std::string iFilename, int iCurrentTimePoint)
54 {
55  this->m_ServerName = iServerName;
56  this->m_Login = iLogin;
57  this->m_Password = iPassword;
58  this->m_ImagingSessionID = iImagingSessionID;
59  this->m_CurrentTimePoint = iCurrentTimePoint;
60  this->m_InFile.open(iFilename.c_str(), std::ifstream::in);
61 }
62 
63 //--------------------------------------------------------------------------
65 {
66 }
67 
68 //--------------------------------------------------------------------------
69 
70 //--------------------------------------------------------------------------
72 {
73  this->OpenDBConnection();
74  IntMapType MapColorIDs;
75  IntMapType MapCellTypeIDs;
76  IntMapType MapSubCellTypeIDs;
77  IntMapType MapCoordIDs;
78  std::string LineContent;
79 
80  LineContent = this->SaveNoTracesEntities(MapColorIDs, MapCellTypeIDs,
81  MapSubCellTypeIDs, MapCoordIDs);
82 
83  this->SaveTracesEntities(MapColorIDs, MapCoordIDs, LineContent, MapCellTypeIDs,
84  MapSubCellTypeIDs);
85  //this->FillContourInfoForVisu(this->m_NewContourIDs);
86  this->CloseDBConnection();
87 }
88 
89 //--------------------------------------------------------------------------
90 
91 //--------------------------------------------------------------------------
93 {
94  this->OpenDBConnection();
95  this->m_NewContourIDs.clear();
96  IntMapType MapColorIDs;
97  IntMapType MapCellTypeIDs;
98  IntMapType MapSubCellTypeIDs;
99  IntMapType MapCoordIDs;
100  std::string LineContent;
101 
102  LineContent = this->SaveNoTracesEntities(MapColorIDs, MapCellTypeIDs,
103  MapSubCellTypeIDs, MapCoordIDs);
104  this->SaveTracesEntities(MapColorIDs, MapCoordIDs, LineContent, MapCellTypeIDs,
105  MapSubCellTypeIDs, true);
106  //this->FillMeshInfoForVisu(this->m_NewMeshIDs);
107  this->CloseDBConnection();
108 }
109 
110 //--------------------------------------------------------------------------
111 
112 //--------------------------------------------------------------------------
114 {
115  this->ImportMeshes();
116 }
117 
118 //--------------------------------------------------------------------------
119 
120 //--------------------------------------------------------------------------
121 std::string GoDBImport::SaveNoTracesEntities(IntMapType & ioMapColorIDs,
122  IntMapType & ioMapCellTypeIDs,
123  IntMapType & ioMapSubCellTypeIDs,
124  IntMapType & ioMapCoordIDs)
125 {
126  std::string LineContent;
127 
128  getline(this->m_InFile, LineContent);
129  while ( !this->IsLineForNumberOfEntities(LineContent) )
130  {
131  getline (this->m_InFile, LineContent);
132  }
133  while ( this->FindFieldName(LineContent) != "NumberOflineage" )
134  {
135  int EntitiesNumber = atoi( this->GetValueForTheLine(LineContent).c_str() );
136  getline(this->m_InFile, LineContent);
137  //if there is nothing to be saved in the database for this group, just go
138  //to the next line in the file:
139  if ( EntitiesNumber != 0 )
140  {
141  if ( this->GetValueForTheLine(LineContent) != "NoValueOnTheLine" )
142  {
143  std::cout << "There was supposed to be only the name of the entity to save,the entity will not be saved";
144  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
145  std::cout << std::endl;
146  return LineContent;
147  }
148  std::string NameEntity = this->FindFieldName(LineContent);
149  if ( NameEntity == "color" )
150  {
151  LineContent = this->SaveImportedEntitiesInDatabase< GoDBColorRow >(
152  EntitiesNumber, ioMapColorIDs);
153  }
154  if ( NameEntity == "celltype" )
155  {
156  LineContent = this->SaveImportedEntitiesInDatabase< GoDBCellTypeRow >(
157  EntitiesNumber, ioMapCellTypeIDs);
158  }
159  if ( NameEntity == "subcellulartype" )
160  {
161  LineContent = this->SaveImportedEntitiesInDatabase< GoDBSubCellTypeRow >(
162  EntitiesNumber, ioMapSubCellTypeIDs);
163  }
164  if ( NameEntity == "coordinate" )
165  {
166  LineContent = this->SaveImportedEntitiesInDatabase< GoDBCoordinateRow >(
167  EntitiesNumber, ioMapCoordIDs);
168  }
169  if ( NameEntity != "color" && NameEntity != "celltype"
170  && NameEntity != "subcellulartype" && NameEntity != "coordinate" )
171  {
172  std::cout << "The name of the entity doesn't correspond to any of the no traces entity";
173  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
174  std::cout << std::endl;
175  }
176  }
177  }
178  return LineContent;
179 }
180 
181 //--------------------------------------------------------------------------
182 
183 //--------------------------------------------------------------------------
184 void GoDBImport::SaveTracesEntities(const IntMapType & iMapColorIDs,
185  const IntMapType & iMapCoordIDs,
186  const std::string & iLineContent,
187  const IntMapType & iMapCellTypeIDs,
188  const IntMapType & iMapSubCellTypeIDs,
189  bool SaveIntensities)
190 {
191  IntMapType MapContourIDs;
192  IntMapType MapMeshIDs;
193  IntMapType MapTrackIDs;
194  IntMapType MapLineageIDs;
195 
196  std::string LineContent = iLineContent;
197 
198  {
199  IntMapType MapIDsSpecificOne;
200  IntMapType MapIDsSpecificTwo;
201 
202  this->SaveTraces< GoDBLineageRow >(iMapColorIDs, iMapCoordIDs,
203  MapLineageIDs, LineContent,
204  this->m_NewLineageIDs, MapLineageIDs,
205  MapIDsSpecificOne, MapIDsSpecificTwo );
206  }
207 
208  {
209  IntMapType MapIDsSpecificOne;
210  IntMapType MapIDsSpecificTwo;
211 
212  this->SaveTraces< GoDBTrackRow >(iMapColorIDs, iMapCoordIDs, MapLineageIDs,
213  LineContent, this->m_NewTracksIDs, MapTrackIDs,
214  MapIDsSpecificOne, MapIDsSpecificTwo );
215 
216  }
217 
218  {
219  this->SaveTraces< GoDBMeshRow >(iMapColorIDs, iMapCoordIDs, MapTrackIDs,
220  LineContent, this->m_NewMeshIDs, MapMeshIDs, iMapCellTypeIDs,
221  iMapSubCellTypeIDs);
222  }
223 
224  if ( SaveIntensities )
225  {
226  this->SaveIntensityForMesh(LineContent, MapMeshIDs, iMapColorIDs);
227  }
228 
229  {
230  IntMapType MapIDsSpecificOne;
231  IntMapType MapIDsSpecificTwo;
232 
233  this->SaveTraces< GoDBContourRow >(iMapColorIDs, iMapCoordIDs, MapMeshIDs,
234  LineContent, this->m_NewContourIDs, MapContourIDs,
235  MapIDsSpecificOne, MapIDsSpecificTwo );
236  }
237 }
238 
239 //--------------------------------------------------------------------------
240 
241 //--------------------------------------------------------------------------
242 std::string GoDBImport::FindFieldName(std::string iLine)
243 {
244  size_t BegName = iLine.find("<", 0) + 1;
245  size_t EndName = iLine.find(">", 0);
246  size_t NameLength = EndName - BegName;
247  std::string oName = iLine.substr(BegName, NameLength);
248 
249  if ( oName.find("/", 0) == 0 )
250  {
251  oName = oName.substr(1);
252  }
253  return oName;
254 }
255 
256 //--------------------------------------------------------------------------
257 
258 //--------------------------------------------------------------------------
259 std::string GoDBImport::GetValueForTheLine(std::string iLine)
260 {
261  size_t BegValue = iLine.find(">", 0) + 1;
262  size_t EndValue = iLine.find("<", BegValue);
263 
264  if ( EndValue != iLine.npos )
265  {
266  size_t ValueLength = EndValue - BegValue;
267  return iLine.substr(BegValue, ValueLength);
268  }
269  return "NoValueOnTheLine";
270 }
271 
272 //--------------------------------------------------------------------------
273 
274 //--------------------------------------------------------------------------
276 {
277  size_t BegValue = iLine.find("NumberOf", 0);
278 
279  if ( BegValue != iLine.npos )
280  {
281  return true;
282  }
283  return false;
284 }
285 
286 //--------------------------------------------------------------------------
287 
288 //--------------------------------------------------------------------------
290 {
292  m_Login, m_Password, "gofiguredatabase");
293 }
294 
295 //--------------------------------------------------------------------------
296 
297 //--------------------------------------------------------------------------
299 {
301 }
302 
303 //--------------------------------------------------------------------------
304 
305 //--------------------------------------------------------------------------
306 void GoDBImport::SaveIntensityForMesh(std::string & ioLineContent,
307  const IntMapType & iMapMeshIDs,
308  const IntMapType & iMapColorIDs)
309 {
310  IntMapType MapChannelIDs;
311  while ( this->FindFieldName(ioLineContent) != "NumberOfchannel" )
312  {
313  getline (this->m_InFile, ioLineContent);
314  }
315 
316  int EntitiesNumber = atoi( this->GetValueForTheLine(ioLineContent).c_str() );
317  getline(this->m_InFile, ioLineContent);
318  if ( EntitiesNumber != 0 )
319  {
320  while ( this->FindFieldName(ioLineContent) != "NumberOfintensity" )
321  {
322  if ( this->GetValueForTheLine(ioLineContent) != "NoValueOnTheLine" )
323  {
324  std::cout << "There was supposed to be only the name of the entity to save,the entity will not be saved";
325  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
326  std::cout << std::endl;
327  }
328  std::string NameEntity = this->FindFieldName(ioLineContent);
329  if ( NameEntity != "channel" )
330  {
331  std::cout << "The name of the entity should be channel but is actually different";
332  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
333  std::cout << std::endl;
334  }
335  else
336  {
337  GoDBChannelRow NewChannel;
338  ioLineContent = this->GetValuesFromInfile< GoDBChannelRow >(NewChannel);
339  this->ReplaceTheFieldWithNewIDs< GoDBChannelRow >(
340  iMapColorIDs, "ColorID", NewChannel);
341  int OldID = NewChannel.GetMapValue<int>("ChannelID");
342  NewChannel.SetField("ChannelID", "0");
343  NewChannel.SetField("ImagingSessionID", this->m_ImagingSessionID);
344  MapChannelIDs[OldID] = NewChannel.SaveInDB(this->m_DatabaseConnector);
345  }
346  }
347  }
348  EntitiesNumber = atoi( this->GetValueForTheLine(ioLineContent).c_str() );
349  getline(this->m_InFile, ioLineContent);
350  if ( EntitiesNumber != 0 )
351  {
352  while ( this->FindFieldName(ioLineContent) != "ExportTraces" )
353  {
354  if ( this->GetValueForTheLine(ioLineContent) != "NoValueOnTheLine" )
355  {
356  std::cout << "There was supposed to be only the name of the entity to save,the entity will not be saved";
357  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
358  std::cout << std::endl;
359  }
360  std::string NameEntity = this->FindFieldName(ioLineContent);
361  if ( NameEntity != "intensity" )
362  {
363  std::cout << "The name of the entity should be channel but is actually different";
364  std::cout << "Debug: In " << __FILE__ << ", line " << __LINE__;
365  std::cout << std::endl;
366  }
367  else
368  {
369  GoDBIntensityRow NewIntensity;
370  ioLineContent = this->GetValuesFromInfile< GoDBIntensityRow >(NewIntensity);
371  this->ReplaceTheFieldWithNewIDs< GoDBIntensityRow >(
372  iMapMeshIDs, "meshID", NewIntensity);
373  this->ReplaceTheFieldWithNewIDs< GoDBIntensityRow >(
374  MapChannelIDs, "ChannelID", NewIntensity);
375  NewIntensity.SetField("IntensityID", "0");
376  NewIntensity.SaveInDB(this->m_DatabaseConnector);
377  }
378  }
379  }
380 }
std::string m_Login
Definition: GoDBImport.h:121
std::string m_ServerName
Definition: GoDBImport.h:119
void ImportContours()
get the data needed from the import file to save the contours listed in it, including the color...
Definition: GoDBImport.cxx:71
manages a map with keys matching fields of the gofiguredatabase Intensity table and values of the map...
std::vector< int > m_NewLineageIDs
Definition: GoDBImport.h:128
GoDBImport(std::string iServerName, std::string iLogin, std::string iPassword, int iImagingSessionID, std::string iFilename, int iCurrentTimePoint)
Definition: GoDBImport.cxx:51
void SaveIntensityForMesh(std::string &ioLineContent, const IntMapType &iMapMeshIDs, const IntMapType &iMapColorIDs)
fill the info needed for the new imported contours to add them in the visu
Definition: GoDBImport.cxx:306
bool CloseDatabaseConnection(vtkMySQLDatabase *DatabaseConnector)
return true if the connection has been closed, false if the connection was already closed ...
std::vector< int > m_NewTracksIDs
Definition: GoDBImport.h:127
vtkMySQLDatabase * OpenDatabaseConnection(std::string ServerName, std::string login, std::string Password, std::string DBName)
std::string m_Password
Definition: GoDBImport.h:120
int SaveInDB(vtkMySQLDatabase *DatabaseConnector)
std::vector< int > m_NewContourIDs
Definition: GoDBImport.h:126
std::map< int, int > IntMapType
Definition: GoDBImport.h:132
std::string FindFieldName(std::string iLine)
Return the name of the field contained in the line.
Definition: GoDBImport.cxx:242
std::vector< int > m_NewMeshIDs
Definition: GoDBImport.h:125
int SaveInDB(vtkMySQLDatabase *DatabaseConnector)
bool IsLineForNumberOfEntities(std::string iLine)
Return true if the line containes "Number Of".
Definition: GoDBImport.cxx:275
vtkMySQLDatabase * m_DatabaseConnector
Definition: GoDBImport.h:118
int m_ImagingSessionID
Definition: GoDBImport.h:122
void SaveTracesEntities(const IntMapType &iMapColorIDs, const IntMapType &iMapCoordIDs, const std::string &iLineContent, const IntMapType &iMapCellTypeIDs, const IntMapType &iMapSubCellTypeIDs, bool SaveIntensities=false)
Get the info for the traces from the import file and from the matching IDs maps previously filled...
Definition: GoDBImport.cxx:184
void CloseDBConnection()
Definition: GoDBImport.cxx:298
std::string GetValueForTheLine(std::string iLine)
Return the value contained in the line and "NoValueOnTheLine" if the line doesn't contain any...
Definition: GoDBImport.cxx:259
void SetField(const std::string &key, const T &value)
convert the value into a string and assign it to the key in the map
Definition: GoDBRow.h:73
void OpenDBConnection()
Definition: GoDBImport.cxx:289
void ImportMeshes()
get the data needed from the import file to save the meshes listed in it, including the color...
Definition: GoDBImport.cxx:92
manages a map with keys matching fields of the gofiguredatabase Channel table and values of the map m...
virtual ~GoDBImport()
Definition: GoDBImport.cxx:64
int m_CurrentTimePoint
Definition: GoDBImport.h:123
void ImportTracks()
get the data needed from the import file to save the tracks listed in it, including the color...
Definition: GoDBImport.cxx:113
std::ifstream m_InFile
Definition: GoDBImport.h:124
std::string SaveNoTracesEntities(IntMapType &ioMapColorIDs, IntMapType &ioMapCellTypeIDs, IntMapType &ioMapSubCellTypeIDs, IntMapType &ioMapCoordIDs)
Get the values from the Infile, save the non traces entities, fill the matching map for old and new I...
Definition: GoDBImport.cxx:121
std::string GetMapValue(const std::string &key)
return the value for the field map[key] after having removed the " at the beginning and at the end of...
Definition: GoDBRow.cxx:174