GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GoDBExport.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 "GoDBExport.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 GoDBExport::GoDBExport(std::string iServerName, std::string iLogin,
52  std::string iPassword, int iImagingSessionID, std::string iFilename)
53 {
54  this->m_ServerName = iServerName;
55  this->m_Login = iLogin;
56  this->m_Password = iPassword;
57  this->m_ImagingSessionID = iImagingSessionID;
58  this->m_outfile.open (iFilename.c_str(), std::ios::out);
59 }
60 
61 //--------------------------------------------------------------------------
63 {
64 }
65 
66 //--------------------------------------------------------------------------
67 
68 //--------------------------------------------------------------------------
70 {
71  this->WriteGeneraleInfo();
72  this->OpenDBConnection();
73  this->WriteOnTheOutputFile( "imagingsession", this->GetImagingSessionInfoFromDB() );
82  this->CloseDBConnection();
83  this->m_outfile << this->GetNameWithSlashBrackets(this->m_NameDocXml);
84 }
85 
86 //--------------------------------------------------------------------------
87 
88 //--------------------------------------------------------------------------
90 {
91  this->WriteGeneraleInfo();
92  this->OpenDBConnection();
93  this->WriteOnTheOutputFile( "imagingsession",
104  this->CloseDBConnection();
105  this->m_outfile << this->GetNameWithSlashBrackets(this->m_NameDocXml);
106 }
107 
108 //--------------------------------------------------------------------------
109 
110 //--------------------------------------------------------------------------
112 {
113  this->m_NameDocXml = "ExportTraces";
114  int VersionNumber = 1;
115  this->m_outfile << "<?xml version=\"1.0\" ?>" << std::endl;
116  this->m_outfile << "<";
117  this->m_outfile << this->m_NameDocXml;
118  this->m_outfile << " version=\"";
119  this->m_outfile << VersionNumber;
120  this->m_outfile << "\">" << std::endl;
121 }
122 
123 //--------------------------------------------------------------------------
124 
125 //--------------------------------------------------------------------------
126 std::vector< std::pair< std::string, std::string > >
128 {
129  std::vector< std::pair< std::string, std::string > > infoImgSession;
130  infoImgSession.push_back( this->GetOneInfoFromDBForImgSession("Name") );
131  infoImgSession.push_back( this->GetOneInfoFromDBForImgSession("CreationDate") );
132  infoImgSession.push_back( this->GetOneInfoFromDBForImgSession("MicroscopeName") );
133 
134  return infoImgSession;
135 }
136 
137 //--------------------------------------------------------------------------
138 
139 //--------------------------------------------------------------------------
140 std::pair< std::string, std::string > GoDBExport::GetOneInfoFromDBForImgSession(
141  std::string iNameInfo)
142 {
143  std::pair< std::string, std::string > OneInfo;
144  OneInfo.first = iNameInfo;
145  OneInfo.second = ListSpecificValuesForOneColumn(
146  this->m_DatabaseConnector, "imagingsession", iNameInfo, "ImagingSessionID",
147  ConvertToString< int >(this->m_ImagingSessionID) ).at(0);
148  return OneInfo;
149 }
150 
151 //--------------------------------------------------------------------------
152 
153 //--------------------------------------------------------------------------
155 {
157  this->m_DatabaseConnector, "contour", "contourID", "imagingsessionID",
158  ConvertToString< int >(this->m_ImagingSessionID) );
159 }
160 
161 //--------------------------------------------------------------------------
162 
163 //--------------------------------------------------------------------------
165 {
166  this->m_VectorContourIDs.clear();
167 }
168 
169 //--------------------------------------------------------------------------
170 
171 //--------------------------------------------------------------------------
173 {
174  if ( !this->m_VectorContourIDs.empty() )
175  {
177  this->m_DatabaseConnector, "contour", "meshID", "contourID",
178  this->m_VectorContourIDs, true, true);
179  }
180 }
181 
182 //--------------------------------------------------------------------------
183 
184 //--------------------------------------------------------------------------
186 {
187  std::vector< FieldWithValue > Conditions(2);
188  FieldWithValue ImgSession = { "ImagingSessionID",
189  ConvertToString< int >(this->m_ImagingSessionID), "=" };
190  FieldWithValue Points = { "Points", "0", "<>" };
191  Conditions[0] = ImgSession;
192  Conditions[1] = Points;
194  "mesh", "meshID", Conditions);
195 }
196 
197 //--------------------------------------------------------------------------
198 
199 //--------------------------------------------------------------------------
201 {
203  this->m_DatabaseConnector, "channel", "ChannelID",
204  "ImagingSessionID", ConvertToString< int >(this->m_ImagingSessionID) );
205 }
206 
207 //--------------------------------------------------------------------------
208 
209 //--------------------------------------------------------------------------
211 {
212  if ( !this->m_VectorMeshIDs.empty() )
213  {
215  this->m_DatabaseConnector, "mesh", "trackID", "meshID",
216  this->m_VectorMeshIDs, true, true);
217  }
218 }
219 
220 //--------------------------------------------------------------------------
221 
222 //--------------------------------------------------------------------------
224 {
225  if ( !this->m_VectorTrackIDs.empty() )
226  {
228  this->m_DatabaseConnector, "track", "lineageID", "trackID",
229  this->m_VectorTrackIDs, true, true);
230  }
231 }
232 
233 //--------------------------------------------------------------------------
234 
235 //--------------------------------------------------------------------------
237 {
242  //no need for channel info when exporting contours at this time:
243  this->m_VectorChannelIDs.clear();
244 }
245 
246 //--------------------------------------------------------------------------
247 
248 //--------------------------------------------------------------------------
250 {
256 }
257 
258 //--------------------------------------------------------------------------
259 
260 //--------------------------------------------------------------------------
262 {
263  std::vector< std::string > TablesNames;
264  std::vector< std::string > FieldNames;
265  std::vector< std::vector< std::string > > VectorTracesIDs;
267  TablesNames, VectorTracesIDs, FieldNames, true);
268  std::vector< std::string > ListColorIDs = std::vector< std::string >();
269  if ( !VectorTracesIDs.empty() )
270  {
271  std::vector< std::string > ColumnNames(1);
272  ColumnNames[0] = "ColorID";
273  ListColorIDs = GetSameFieldsFromSeveralTables(
274  this->m_DatabaseConnector, ColumnNames, TablesNames, FieldNames, VectorTracesIDs);
275  }
276  this->WriteTableInfoFromDB< GoDBColorRow >(ListColorIDs);
277 }
278 
279 //--------------------------------------------------------------------------
280 
281 //--------------------------------------------------------------------------
283 {
284  std::vector< std::string > ListCellTypeIDs = std::vector< std::string >();
285  std::vector< std::string > ListSubCellTypeIDs = std::vector< std::string >();
286  if ( !this->m_VectorMeshIDs.empty() )
287  {
288  ListCellTypeIDs = ListSpecificValuesForOneColumn(
289  this->m_DatabaseConnector, "mesh", "CellTypeID", "meshID",
290  this->m_VectorMeshIDs, true, true);
291 
292  ListSubCellTypeIDs =
294  "mesh", "SubCellularID", "meshID", this->m_VectorMeshIDs, true, true);
295  }
296  this->WriteTableInfoFromDB< GoDBCellTypeRow >(ListCellTypeIDs);
297  this->WriteTableInfoFromDB< GoDBSubCellTypeRow >(ListSubCellTypeIDs);
298 }
299 
300 //--------------------------------------------------------------------------
301 
302 //--------------------------------------------------------------------------
304 {
305  std::vector< std::string > TablesNames;
306  std::vector< std::string > FieldNames;
307  std::vector< std::vector< std::string > > VectorTracesIDs;
308  this->GetVectorsTableNamesTracesIDsAndFields(TablesNames,
309  VectorTracesIDs, FieldNames);
310  std::vector< std::string > ColumnNames(2);
311  ColumnNames[0] = "CoordIDMax";
312  ColumnNames[1] = "CoordIDMin";
313  std::vector< std::string > ListCoordIDs = std::vector< std::string >();
314  if ( !VectorTracesIDs.empty() )
315  {
316  ListCoordIDs = GetSameFieldsFromSeveralTables(
317  this->m_DatabaseConnector, ColumnNames, TablesNames, FieldNames, VectorTracesIDs);
318  }
319 
320  this->WriteTableInfoFromDB< GoDBCoordinateRow >(ListCoordIDs);
321 }
322 
323 //--------------------------------------------------------------------------
324 
325 //--------------------------------------------------------------------------
327 {
328  this->WriteTableInfoFromDB< GoDBLineageRow >(this->m_VectorLineageIDs);
329 }
330 
331 //--------------------------------------------------------------------------
332 
333 //--------------------------------------------------------------------------
335 {
336  this->WriteTableInfoFromDB< GoDBTrackRow >(this->m_VectorTrackIDs);
337 }
338 
339 //--------------------------------------------------------------------------
340 
341 //--------------------------------------------------------------------------
343 {
344  this->WriteTableInfoFromDB< GoDBMeshRow >(this->m_VectorMeshIDs);
345 }
346 
347 //--------------------------------------------------------------------------
348 
349 //--------------------------------------------------------------------------
351 {
352  this->WriteTableInfoFromDB< GoDBChannelRow >(this->m_VectorChannelIDs);
353 }
354 
355 //--------------------------------------------------------------------------
356 
357 //--------------------------------------------------------------------------
359 {
360  std::vector< std::string > VectorIntensityIDs = std::vector< std::string >();
361  if ( !this->m_VectorMeshIDs.empty() )
362  {
363  VectorIntensityIDs =
365  this->m_DatabaseConnector, "IntensityID", "intensity",
366  "meshID", this->m_VectorMeshIDs, "ChannelID", this->m_VectorChannelIDs);
367  }
368  this->WriteTableInfoFromDB< GoDBIntensityRow >(VectorIntensityIDs);
369 }
370 
371 //--------------------------------------------------------------------------
372 
373 //--------------------------------------------------------------------------
375 {
376  this->WriteTableInfoFromDB< GoDBContourRow >(m_VectorContourIDs);
377 }
378 
379 //--------------------------------------------------------------------------
380 
381 //--------------------------------------------------------------------------
382 void GoDBExport::WriteOnTheOutputFile(std::string iNameOfEntity,
383  std::vector< std::pair< std::string, std::string > > iInfoToWrite)
384 {
385  this->AddTabulation();
386  this->m_outfile << GetNameWithBrackets(iNameOfEntity) << std::endl;
387  std::vector< std::pair< std::string, std::string > >::iterator iter =
388  iInfoToWrite.begin();
389  while ( iter != iInfoToWrite.end() )
390  {
391  this->AddTabulation();
392  this->AddTabulation();
393  this->m_outfile << this->GetNameWithBrackets(iter->first);
394  this->m_outfile << iter->second;
395  this->m_outfile << this->GetNameWithSlashBrackets(iter->first) << std::endl;
396  ++iter;
397  }
398  this->AddTabulation();
399  this->m_outfile << GetNameWithSlashBrackets(iNameOfEntity) << std::endl;
400 }
401 
402 //--------------------------------------------------------------------------
403 
404 //--------------------------------------------------------------------------
405 void GoDBExport::WriteNumberOfEntities(std::string iNameOfEntity, size_t iNumber)
406 {
407  this->AddTabulation();
408  std::string NameToWrite = "NumberOf";
409  NameToWrite += iNameOfEntity;
410  this->m_outfile << this->GetNameWithBrackets(NameToWrite);
411  this->m_outfile << iNumber;
412  this->m_outfile << this->GetNameWithSlashBrackets(NameToWrite) << std::endl;
413 }
414 
415 //--------------------------------------------------------------------------
416 
417 //--------------------------------------------------------------------------
418 std::string GoDBExport::GetNameWithBrackets(std::string iName)
419 {
420  std::stringstream NameWithBrackets;
421 
422  NameWithBrackets << "<";
423  NameWithBrackets << iName;
424  NameWithBrackets << ">";
425  return NameWithBrackets.str();
426 }
427 
428 //--------------------------------------------------------------------------
429 
430 //--------------------------------------------------------------------------
431 std::string GoDBExport::GetNameWithSlashBrackets(std::string iName)
432 {
433  std::stringstream NameWithBrackets;
434 
435  NameWithBrackets << "</";
436  NameWithBrackets << iName;
437  NameWithBrackets << ">";
438  return NameWithBrackets.str();
439 }
440 
441 //--------------------------------------------------------------------------
442 
443 //--------------------------------------------------------------------------
445 {
446  this->m_outfile << " ";
447 }
448 
449 //--------------------------------------------------------------------------
450 
451 //--------------------------------------------------------------------------
453 {
455  m_Login, m_Password, "gofiguredatabase");
456 }
457 
458 //--------------------------------------------------------------------------
459 
460 //--------------------------------------------------------------------------
462 {
464 }
465 
466 //--------------------------------------------------------------------------
467 
468 //--------------------------------------------------------------------------
470  std::vector< std::string > & ioVectorTableNames,
471  std::vector< std::vector< std::string > > & ioVectorTracesIDs,
472  std::vector< std::string > & ioVectorFields, bool IncludeChannelIDs)
473 {
474  if ( !this->m_VectorContourIDs.empty() )
475  {
476  ioVectorTableNames.push_back("contour");
477  ioVectorFields.push_back("contourID");
478  ioVectorTracesIDs.push_back(this->m_VectorContourIDs);
479  }
480  if ( !this->m_VectorMeshIDs.empty() )
481  {
482  ioVectorTableNames.push_back("mesh");
483  ioVectorFields.push_back("meshID");
484  ioVectorTracesIDs.push_back(this->m_VectorMeshIDs);
485  }
486  if ( !this->m_VectorTrackIDs.empty() )
487  {
488  ioVectorTableNames.push_back("track");
489  ioVectorFields.push_back("trackID");
490  ioVectorTracesIDs.push_back(this->m_VectorTrackIDs);
491  }
492  if ( !this->m_VectorLineageIDs.empty() )
493  {
494  ioVectorTableNames.push_back("lineage");
495  ioVectorFields.push_back("lineageID");
496  ioVectorTracesIDs.push_back(this->m_VectorLineageIDs);
497  }
498  if ( IncludeChannelIDs )
499  {
500  if ( !this->m_VectorChannelIDs.empty() )
501  {
502  ioVectorTableNames.push_back("channel");
503  ioVectorFields.push_back("ChannelID");
504  ioVectorTracesIDs.push_back(this->m_VectorChannelIDs);
505  }
506  }
507 }
508 
509 //--------------------------------------------------------------------------
510 
511 //--------------------------------------------------------------------------