read string

HI.
I have text file like this:
TATAGTTGATTGTCTGCGTCATGTTCTTCTTCTGTCTTATGACGGAAAAGTGAGTTGGAGTCGTGTGGCGTTCAGGT
and I need to count how many times they are combinations of A, T, G, C.

First counter is for how many times solo lether is in file and second should be combinations.

I dont know how to look at next char.
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
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

union myCounter 
{
	unsigned int p0[4];
	unsigned int p1[16];

	struct
	{
		unsigned int A;
		unsigned int G;
		unsigned int T;
		unsigned int C;
	};
};

void readFile(string fileName, string &line)
{
	ifstream file(fileName, ios::in);
	if (!file.is_open())
	{
		cout << "Error when opening file." << endl;
	}
	file >> line;
	file.close();
}

void countBase(string &line, myCounter &counter)
{
	counter.A = counter.C = counter.G = counter.T = 0;
	for (const auto &x : line)
	{
		if (x == 'A') counter.A++;
		if (x == 'G') counter.G++;
		if (x == 'T') counter.T++;
		if (x == 'C') counter.C++;
	}
}

void countBases(string &line, myCounter &counter)
{
	counter.A = counter.C = counter.G = counter.T = 0;
	for ( auto &x : line)
	{
		cout << x << " " <<  endl;
	}
}


int main()
{
	string line = "";
	readFile("dataDNA.txt", line);
	
	myCounter counter;
	countBase (line, counter);
	cout << "Number of occurrences of Adenine: " << counter.A << endl;
	cout << "Number of occurrences of Guanine: " << counter.G << endl;
	cout << "Number of occurrences of Thymine: " << counter.T << endl;
	cout << "Number of occurrences of Cytosine: " << counter.C << endl;

	countBases(line, counter);
	return 0;
}
Line 9: Why are you using a union?

Lines 11-12: These two arrays overlay each other. I'm not clear why you're declaring p0 and p1 at all as they are not used in your program.

Line 49: If you're trying to count adjacent pairs, you might be be better off with a traditional counted for loop. By using a subscript, you can easily refer to line[i] and line[i+1].



Honestly. I dont have any idea what you are talking abouth in line 49.
Line 9 and Line 11-12 is something from class where my teacher say something abouth this.
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
struct myCounter 
{   unsigned int A;
	unsigned int G;
	unsigned int T;
	unsigned int C;
	unsigned int AG;
	unsigned int AT;
	unsigned int AC;
	unsigned int AA;
	unsigned int GA;
	unsigned int GG;
	unsigned int GT;
	unsigned int GC;
	unsigned int TA;
	unsigned int TG;
	unsigned int TT;
	unsigned int TC;
	unsigned int CA;
	unsigned int CG;
	unsigned int CT;
	unsigned int CC;
};
// ...
void countBases(const string &line, myCounter &counter)
{   //  A counted for loop
    for (size_t i=0; i<line.size()-1; i+=2)
    {   if (line[i] == 'A' && line[i+1] == 'A') counter.AA++;
	    if (line[i] == 'A' && line[i+1] == 'C') counter.AC++;
	    if (line[i] == 'A' && line[i+1] == 'G') counter.AG++;
	    if (line[i] == 'A' && line[i+1] == 'T') counter.AT++;
	    if (line[i] == 'C' && line[i+1] == 'A') counter.CA++;
	    if (line[i] == 'C' && line[i+1] == 'C') counter.CC++;
	    if (line[i] == 'C' && line[i+1] == 'G') counter.CG++;
	    if (line[i] == 'C' && line[i+1] == 'T') counter.CT++;
	    if (line[i] == 'G' && line[i+1] == 'A') counter.GA++;
	    if (line[i] == 'G' && line[i+1] == 'C') counter.GC++;
	    if (line[i] == 'G' && line[i+1] == 'G') counter.GG++;
	    if (line[i] == 'G' && line[i+1] == 'T') counter.GT++;
	    if (line[i] == 'T' && line[i+1] == 'A') counter.TA++;
	    if (line[i] == 'T' && line[i+1] == 'C') counter.TC++;
	    if (line[i] == 'T' && line[i+1] == 'G') counter.TG++;
	    if (line[i] == 'T' && line[i+1] == 'T') counter.TT++;	    
	}
}


I'll leave the printing to you.
Last edited on
Topic archived. No new replies allowed.