Advertisement
Ashanmaril

DNA Analysis

Nov 18th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. //
  2. //  a5q1.cpp
  3. //  DNA Analysis
  4. //
  5. //  Created by Hayden Lueck on 2014-11-17.
  6. //  Copyright (c) 2014 Hayden Lueck. All rights reserved.
  7. //
  8. // dna.txt: http://pastebin.com/jshtBzX5
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <fstream>
  13.  
  14. void SetGene(char[]); //sets genes from input file
  15. void PrintGene(char[]); //prints out a gene (used for testing, unused in program)
  16. bool SameGene(char[], char[], int); //checks if 2 genes are the same
  17. std::string SickleType(char[], char[]); //checks whether person is normal, anemic, or a carrier
  18. std::string AreTheyRelated(char[], char[], char[], char[], int); //first 2 for first person's genes,
  19.                                                      //second 2 for second person's genes, int for size
  20. std::ifstream inFile; //declare for input
  21. std::ofstream outFile; //declare for output
  22.  
  23. const int GENE_SIZE = 444; //constant integer for size of genes
  24.  
  25. int main() {
  26.     /***** bind input and output files *****/
  27.     inFile.open("dna.txt");
  28.     outFile.open("DNAanalysis.txt");
  29.    
  30.     /****** check files exist ******/
  31.     if(!inFile){
  32.         std::cout << "Cannot find input file dna.txt Exiting..." << std::endl;
  33.         return 1;
  34.     }
  35.     if(!outFile){
  36.         std::cout << "Cannot find output file DNAanalysis.txt Exiting..." << std::endl;
  37.         return 2;
  38.     }
  39.    
  40.     /***** declare gene arrays *****/
  41.     char gene1A[GENE_SIZE];
  42.     char gene2A[GENE_SIZE];
  43.     char gene1B[GENE_SIZE];
  44.     char gene2B[GENE_SIZE];
  45.     char gene1C[GENE_SIZE];
  46.     char gene2C[GENE_SIZE];
  47.     char gene1D[GENE_SIZE];
  48.     char gene2D[GENE_SIZE];
  49.    
  50.     /***** set genes *****/
  51.     SetGene(gene1A);
  52.     SetGene(gene2A);
  53.     SetGene(gene1B);
  54.     SetGene(gene2B);
  55.     SetGene(gene1C);
  56.     SetGene(gene2C);
  57.     SetGene(gene1D);
  58.     SetGene(gene2D);
  59.    
  60.     /***** check if genes are normal/sickle ******/
  61.     outFile << "Person A is " << SickleType(gene1A, gene2A) << std::endl;
  62.     outFile << "Person B is " << SickleType(gene1B, gene2B) << std::endl;
  63.     outFile << "Person C is " << SickleType(gene1C, gene2C) << std::endl;
  64.     outFile << "Person D is " << SickleType(gene1D, gene2D) << std::endl;
  65.    
  66.     outFile << std::endl;
  67.    
  68.     /***** related people *****/
  69.     outFile << "Person A " << AreTheyRelated(gene1A, gene2A, gene1B, gene2B, GENE_SIZE)
  70.               << "person B" << std::endl;
  71.     outFile << "Person A " << AreTheyRelated(gene1A, gene2A, gene1C, gene2C, GENE_SIZE)
  72.               << "person C" << std::endl;
  73.     outFile << "Person A " << AreTheyRelated(gene1A, gene2A, gene1D, gene2D, GENE_SIZE)
  74.               << "person D" << std::endl;
  75.     outFile << "Person B " << AreTheyRelated(gene1B, gene2B, gene1C, gene2C, GENE_SIZE)
  76.               << "person C" << std::endl;
  77.     outFile << "Person B " << AreTheyRelated(gene1B, gene2B, gene1D, gene2D, GENE_SIZE)
  78.               << "person D" << std::endl;
  79.     outFile << "Person C " << AreTheyRelated(gene1C, gene2C, gene1D, gene2D, GENE_SIZE)
  80.               << "person D" << std::endl;
  81.    
  82.     /***** close input/output files *****/
  83.     inFile.close();
  84.     outFile.close();
  85.    
  86.     return 0;
  87. }
  88.  
  89. void PrintGene(char gene[])
  90. {
  91.     for(int i = 0; i < GENE_SIZE; i++) //go through 444 times
  92.     {
  93.         outFile << gene[i]; //print out each letter in the gene
  94.     }
  95.     outFile << std::endl;
  96. }
  97.  
  98. void SetGene(char gene[])
  99. {
  100.     for (int i = 0; i < GENE_SIZE; i++) //go through 444 times
  101.     {
  102.         inFile.get(gene[i]); //get the next character in the file, set it to the char in the array
  103.     }
  104. }
  105.  
  106. bool SameGene(char gene1[], char gene2[], int size)
  107. {
  108.     bool sameGene = false; //prime sameGene
  109.     for(int i = 0; i < size; i++) //go through 444 times
  110.     {
  111.         if(gene1[i] == gene2[i]) //if current letter in gene1 is same as gene2
  112.             sameGene = true; //set to true, go through again with next letter to make sure it's still true
  113.         else //if one letter is off
  114.         {
  115.             sameGene = false; //set to false
  116.             break; //break out of loop
  117.         }
  118.     }
  119.     return sameGene; //return true/false value of sameGene
  120. }
  121.  
  122. std::string SickleType(char gene1[], char gene2[])
  123. {
  124.     bool geneOneSickle = false, geneTwoSickle = false; //set gene1 and gene2 to not sickle
  125.    
  126.     if(gene1[19] == 'T') //if 20th char is T
  127.         geneOneSickle = true; //gene is sickle
  128.     if(gene2[19] == 'T') //if 20th char is T
  129.         geneTwoSickle = true; //gene is sickle
  130.    
  131.     if(geneOneSickle || geneTwoSickle) //if either is sickle
  132.     {
  133.         if(geneTwoSickle && geneTwoSickle) //check if both are sickle, if they are...
  134.             return "a carrier"; //they're a carrier
  135.         else //only one is sickle
  136.             return "anemic"; //they're anemic
  137.     }
  138.     else //neither genes are sickle
  139.         return "normal"; //the person is normal
  140. }
  141.  
  142. std::string AreTheyRelated(char person1Gene1[], char person1Gene2[], char person2Gene1[], char person2Gene2[], int size)
  143. {
  144.     if(SameGene(person1Gene1, person2Gene1, size) ||
  145.        SameGene(person1Gene1, person2Gene2, size) ||
  146.        SameGene(person1Gene2, person2Gene1, size) ||
  147.        SameGene(person1Gene2, person2Gene2, size)) //compare all genes, check if any are the same
  148.     {
  149.         return " is related to "; //if true, they're related
  150.     }
  151.     else //if false...
  152.         return " is not related to "; //they're not related
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement