1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int A=0,T=0,G=0,C=0;
const double tot = A+T+C+G;
bool countNucleotides(string filename);
void formatOutput(ostream& out, string nucleotideName, int nucleotideCount, double nucleotidePercentage);
void report();
int main()
{
bool success = countNucleotides("dnaSequence.txt");
if (!success){
cout << "Error opening file.";
return 1;
}
//constant for the total
const double tot = A+T+C+G;
//output with totals and percentages - spaced evenly - correct format
}
bool countNucleotides(string filename)
{
ifstream dnaSeq(filename);//open txt file
if (!dnaSeq){
return false;
}
for (char ch; dnaSeq.get(ch); ) {
//char variable + get chars
if (ch == 'A') //if A
++A; //increment
else if (ch == 'T') //if T
++T; //increment
else if (ch == 'C') //if C
++C; //increment
else if (ch == 'G') //if G
++G; //increment
while (ch == ' ') //non Nucleotides
cout << "Invalid Nucleotide"; //if not listed above
}
//close file
dnaSeq.close();
return true;
}
void formatOutput(ostream& out, string nucleotideName, int nucleotideCount, double nucleotidePercentage) {
if (nucleotidePercentage < 0.0)
cout << nucleotideName << nucleotideCount;
else
out << nucleotideName << nucleotideCount << setprecision(4) << setw(10) << nucleotidePercentage << "%";
}
void report(){
bool countNucleotides(string filename);
formatOutput(cout, "DNA sequence analysis: ", A+T+G+C, -1);
formatOutput(cout, "\nAdenine: ", A, A/tot*100);
formatOutput(cout, "\nThymine: ", T, T/tot*100);
formatOutput(cout, "\nCytosine: ", C, C/tot*100);
formatOutput(cout, "\nGuanine: ", G, G/tot*100);
}
| |