Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. //====================================================================
  2. // GeneticsSimDataParser.cpp
  3. // Implementation file for the data parser.
  4. // Author: Dr. Rick Coleman
  5. // Date: December 2009
  6. //====================================================================
  7. #pragma warning(disable : 4996)
  8. #include "pch.h"
  9. #include "GeneticsSimDataParser.h"
  10. #include <string.h>
  11.  
  12. //-----------------------------------
  13. // Constructor
  14. //-----------------------------------
  15. GeneticsSimDataParser::GeneticsSimDataParser(const char *fileName)
  16. {
  17. // Initialize everything
  18. char line[128];
  19. m_iNumGenes = 0;
  20. m_iNextGeneNumber = 0;
  21. m_iNextParentNumber = 0;
  22. strcpy_s(m_sFileName, fileName);
  23.  
  24. // Open the data file for reading
  25. inFile = new ifstream(); // Create an ifstream object
  26. inFile->open(fileName, fstream::in); // Open it
  27. if (inFile->is_open()) // Make sure it is open
  28. {
  29. // Get the basic information
  30. while (getNextLine(line, 127))
  31. {
  32. if (strcmp(line, "<GENE>") == 0)
  33. {
  34. m_iNumGenes++; // Count all wells
  35. }
  36. else if (strcmp(line, "<GENUS>") == 0)
  37. {
  38. getNextLine(line, 127); // Read the genus string
  39. strcpy_s(m_sGenus, line); // Save the genus name
  40. }
  41. else if (strcmp(line, "<SPECIES>") == 0)
  42. {
  43. getNextLine(line, 127); // Read the species string
  44. strcpy_s(m_sSpecies, line); // Save the species name
  45. }
  46. else if (strcmp(line, "<COMMON_NAME>") == 0)
  47. {
  48. getNextLine(line, 127); // Read the common name string
  49. strcpy_s(m_sCommonName, line); // Save the common name
  50. }
  51. }
  52. inFile->close(); // Close the file
  53. delete inFile; // Destroy the ifstream object
  54. }
  55. else
  56. {
  57. cout << "Failed to open file\n";
  58. cout << "Program terminated.\n\n";
  59. exit(0);
  60. }
  61. }
  62.  
  63. //-----------------------------------
  64. // Destructor
  65. //-----------------------------------
  66. GeneticsSimDataParser::~GeneticsSimDataParser()
  67. {
  68. }
  69.  
  70. //-----------------------------------
  71. // Get the number of genes
  72. //-----------------------------------
  73. int GeneticsSimDataParser::getGeneCount()
  74. {
  75. return m_iNumGenes;
  76. }
  77.  
  78. //-----------------------------------
  79. // Get the genus name
  80. //-----------------------------------
  81. char *GeneticsSimDataParser::getGenus()
  82. {
  83. return m_sGenus;
  84. }
  85.  
  86. //-----------------------------------
  87. // Get the species name
  88. //-----------------------------------
  89. char *GeneticsSimDataParser::getSpecies()
  90. {
  91. return m_sSpecies;
  92. }
  93.  
  94. //-----------------------------------
  95. // Get the full scientific name
  96. //-----------------------------------
  97. char *GeneticsSimDataParser::getScientificName()
  98. {
  99. // Build the full name just in case it hasn't been done
  100. sprintf_s(m_sSciName, "%s %s", m_sGenus, m_sSpecies);
  101. return m_sSciName;
  102. }
  103.  
  104. //-----------------------------------
  105. // Get the common name
  106. //-----------------------------------
  107. char *GeneticsSimDataParser::getCommonName()
  108. {
  109. return m_sCommonName;
  110. }
  111.  
  112. //--------------------------------------------------------------------------
  113. // Get data on the next gene
  114. // Args: *trait - pointer to a character array to hold the trait name
  115. // *domDesc - pointer to a character array to hold the name of the
  116. // dominant allele name
  117. // *domSymbol - pointer to a char variable to hold the dominant
  118. // trait letter symbol
  119. // *recDesc - pointer to a character array to hold the name of the
  120. // recessive allele name
  121. // *recSymbol - pointer to a char variable to hold the recessive
  122. // trait letter symbol
  123. //--------------------------------------------------------------------------
  124. bool GeneticsSimDataParser::getGeneData(char *trait, char *domDesc, char *domSymbol,
  125. char *recDesc, char *recSymbol)
  126. {
  127. int gNum = 0;
  128. char line[128];
  129. // Reopen the file
  130. inFile = new ifstream();
  131. inFile->open(m_sFileName, fstream::in);
  132. if (inFile->is_open())
  133. {
  134. // Read to the the current Gene count
  135. while (getNextLine(line, 127))
  136. {
  137. if (strcmp(line, "<GENE>") == 0) // Got one
  138. {
  139. if (gNum == m_iNextGeneNumber)
  140. {
  141. // Get data on this one
  142. while (getNextLine(line, 127))
  143. {
  144. // Get the gene trait name
  145. if (strcmp(line, "<GENE_TRAIT>") == 0)
  146. {
  147. if (getNextLine(line, 127))
  148. {
  149. strcpy(trait, line); // Set the trait name
  150. }
  151. else
  152. return false; // Oops!
  153. }
  154. else if (strcmp(line, "<DOMINANT_ALLELE>") == 0)
  155. {
  156. if (getNextLine(line, 127))
  157. {
  158. strcpy(domDesc, line); // Set the dominant allele name
  159. }
  160. else
  161. return false; // Oops!
  162. }
  163. else if (strcmp(line, "<DOMINANT_SYMBOL>") == 0)
  164. {
  165. if (getNextLine(line, 127))
  166. {
  167. *domSymbol = line[0]; // Set the dominant allele symbol
  168. }
  169. else
  170. return false; // Oops!
  171. }
  172. else if (strcmp(line, "<RECESSIVE_ALLELE>") == 0)
  173. {
  174. if (getNextLine(line, 127))
  175. {
  176. strcpy(recDesc, line); // Set the recessive allele name
  177. }
  178. else
  179. return false; // Oops!
  180. }
  181. else if (strcmp(line, "<RECESSIVE_SYMBOL>") == 0)
  182. {
  183. if (getNextLine(line, 127))
  184. {
  185. *recSymbol = line[0]; // Set the dominant allele symbol
  186. }
  187. else
  188. return false; // Oops!
  189. }
  190. else if (strcmp(line, "</GENE>") == 0)
  191. {
  192. m_iNextGeneNumber++; // Increment for next call to this function
  193. inFile->close();
  194. delete inFile; // Delete the istream object not the file
  195. return true; // Got it
  196. }
  197. } // end while
  198. } // end if(gNum == nextGene)
  199. else
  200. {
  201. gNum++; // Check the next one
  202. }
  203. }
  204. }
  205. inFile->close();
  206. delete inFile; // Delete the istream object not the file
  207. } // end if file open
  208. return false; // If we get here we have got all the genes
  209. }
  210.  
  211. //--------------------------------------------------------------------------
  212. // Get data on the next parent
  213. // Args: *genotype - pointer to a character array to hold the genotype
  214. // description as a string, e.g. "Tt Ss Ww Cc". This
  215. // is a pea plant with the phenotype of tall with
  216. // wrinkled, green seeds and purple flowers. Its
  217. // genotype if heterozygous tall, heterozygous wrinkled
  218. // seeds, heterozygous green seeds, and heterozygous
  219. // purple flowers.
  220. //--------------------------------------------------------------------------
  221. bool GeneticsSimDataParser::getParentGenotype(char *genotype)
  222. {
  223. int pNum = 0;
  224. char line[128];
  225. // Limit to only two parents in any one test
  226. if (m_iNextParentNumber >= 2) return false;
  227.  
  228. // Reopen the file
  229. inFile = new ifstream();
  230. inFile->open(m_sFileName, fstream::in);
  231. if (inFile->is_open())
  232. {
  233. // Read to the the current sensor count
  234. while (getNextLine(line, 127))
  235. {
  236. if (strcmp(line, "<PARENT>") == 0) // Got one
  237. {
  238. if (pNum == m_iNextParentNumber)
  239. {
  240. // Get data on this one
  241. while (getNextLine(line, 127))
  242. {
  243. // Get the sensor type
  244. if (strcmp(line, "<GENOTYPE>") == 0)
  245. {
  246. if (getNextLine(line, 127)) // Read the genotype string
  247. {
  248. strcpy(genotype, line);
  249. }
  250. else
  251. return false; // Oops!
  252. }
  253. else if (strcmp(line, "</PARENT>") == 0)
  254. {
  255. m_iNextParentNumber++; // Increment for next call to this function
  256. inFile->close();
  257. delete inFile; // Delete the istream object not the file
  258. return true; // Got one
  259. }
  260. } // end while
  261. } // end if(pNum == nextParentNum)
  262. else
  263. {
  264. pNum++; // Check the next one
  265. }
  266. }
  267. }
  268. inFile->close();
  269. delete inFile; // Delete the istream object not the file
  270. } // end if file open
  271. return false; // If we get here we have got all the parents or failed to open file
  272. }
  273.  
  274. //------------------------------------------------
  275. // Function: getNextLine()
  276. // Purpose: Reads lines from a file and places
  277. // them in buffer, removing any leading white
  278. // space. Skips blank lines. Ignors comment
  279. // lines starting with <!-- and ending with -->
  280. //
  281. // Args: buffer -- char array to read line into.
  282. // n -- length of buffer.
  283. // Returns: True if a line was successfully read,
  284. // false if the end of file was encountered.
  285. // Notes: Function provided by instructor.
  286. //------------------------------------------------
  287. bool GeneticsSimDataParser::getNextLine(char *buffer, int n)
  288. {
  289. bool done = false;
  290. char tempBuf[128];
  291. char *temp;
  292. while (!done)
  293. {
  294. inFile->getline(tempBuf, n); // Read a line from the file
  295.  
  296. if (inFile->good()) // If a line was successfully read check it
  297. {
  298. if (strlen(tempBuf) == 0) // Skip any blank lines
  299. continue;
  300. else if (strncmp(tempBuf, "<!--", 4) == 0) // Skip comment lines
  301. continue;
  302. else done = true; // Got a valid data line so return with this line
  303. }
  304. else
  305. {
  306. strcpy(buffer, ""); // Clear the buffer array
  307. return false; // Flag end of file
  308. }
  309. } // end while
  310. // Remove white space from end of string
  311. temp = &tempBuf[strlen(tempBuf)]; // point to closing \0
  312. temp--; // back up 1 space
  313. while (isspace(*temp))
  314. {
  315. *temp = '\0'; // Make it another NULL terminator
  316. temp--; // Back up 1 char
  317. }
  318. // Remove white space from front of string
  319. temp = tempBuf;
  320. while (isspace(*temp)) temp++; // Skip leading white space
  321. // Copy remainder of string into the buffer
  322. strcpy(buffer, temp);
  323. return true; // Flag a successful read
  324. }
  325.  
  326.  
  327. GeneticsSimDataParser::GeneticsSimDataParser()
  328. {
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement